开发者

Multiple Datasources in DataGrid (ASP.NET)

开发者 https://www.devze.com 2022-12-19 08:53 出处:网络
I wonder开发者_开发百科 if we can implement and fetch data from different datasources into a DataGrid.

I wonder开发者_开发百科 if we can implement and fetch data from different datasources into a DataGrid. Let's say for example i have a 3 stored procedures:

What I did is drag 3 datasources and configured it in each stored procedures.

1st stored procedures : returns @id and @name 2nd stored procedures : returns @name, @server and @location 3rd stored procedures : returns @age and @years

and I want only to show @id from 1st SPROC, @server from 2nd SRPOC and @age from 3rd SPROC, and bind it to one DataGrid.

Thanks.


You can't set several datasources on any control.

Why not create a stored procedure that retrieves all the information you want in one go? Then you can bind the result to your DataGrid.


I would agree with Oded. Having one source makes it simple and easy to manage/debug. But if you prefer to have multiplet data sources, I would populate a dataset with these data sources, generate a dataview from this dataset (selecting the appropriate data) and bind the view to the datagrid. An example from here

private void btnLoadData_Click(object sender, System.EventArgs e)
{
    string connectionString = "server=P-III; database=programmersheaven;" +
                      "uid=sa; pwd=;";
    SqlConnection conn = new SqlConnection(connectionString);
    string cmdString = "SELECT * FROM article";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "article");
    cmdString = "SELECT * FROM author";
    dataAdapter = new SqlDataAdapter(cmdString, conn);
    dataAdapter.Fill(ds, "author");
    DataRelation relation = new DataRelation("ArtAuth", 
            ds.Tables["author"].Columns["authorId"], 
            ds.Tables["article"].Columns["authorId"] 
            );
    ds.Relations.Add(relation);
    DataView dv = new DataView(ds.Tables["author"]);
    dgDetails.DataSource = dv;
}


You can perform an inner join query in the sqldatasource at the selectcommand property to select what you want from the DB, then access the datagrid.datasourceID from the sqldatasource control.

0

精彩评论

暂无评论...
验证码 换一张
取 消