Sherwin
Sherwin

Reputation: 377

Multiple Datasources in DataGrid (ASP.NET)

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.

Upvotes: 1

Views: 3167

Answers (3)

Sunny Awa Okoro
Sunny Awa Okoro

Reputation: 11

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.

Upvotes: 1

ram
ram

Reputation: 11626

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;
}

Upvotes: 1

Oded
Oded

Reputation: 499382

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.

Upvotes: 4

Related Questions