|
i've to show data from 3 tables in a datagird, the problem is
how to fill dataadapter with Fill method , shud i take 3 dataadapter objects and 3 dataset objects???? cud anybody suggest me.?????
|
|
|
You achieve this using one adaptor and one dataset. In side dataset, u can store multiple tables. Use Fill method to fill each table.
Once you get all three tables in dataset, create a you custom table using Datatable and merge three tables. Then create dataview for that datatable. Use this dataview as datagrid datasource. Following code snap would help to understand…
// Sql connection..
SqlConnection con = new SqlConnection(connectionString);
// Create Data Adaptor
SqlDataAdapter DAEmp = new SqlDataAdapter();
// Sql command for selecting tables
SqlCommand Dcom = new SqlCommand();
Dcom.CommandText = @"Select * from table1"; // first table
Dcom.Connection = con;
DAEmp.SelectCommand = Dcom;
// create Data set
DataSet DSEmp = new DataSet();
DAEmp.Fill(DSEmp,"Table1"); // Filling first table
Dcom.CommandText = @"select * from table2"; // Second table
DAEmp.SelectCommand = Dcom;
DAEmp.Fill(DSEmp,"Table2"); // Filling second table
Dcom.CommandText = @"select * from table3"; // Third table
DAEmp.SelectCommand = Dcom;
DAEmp.Fill(DSEmp,"Table3"); // Filling third table
// Create table for merger all three tables
DataTable tb = new DataTable();
// Data column to add in table
DataColumn dc = new DataColumn("Col1",typeof(string));
DataColumn dc1 = new DataColumn("Col2",typeof(string));
tb.Columns.Add(dc);
tb.Columns.Add(dc1);
// Fetching all three table – to insert row in table
for ( int k = 0; k < DSEmp.Tables.Count; k++ )
{
// Fetching all row of each table – to insert row in table
for ( int i = 0; i < DSEmp.Tables[k].Rows.Count ; i++ )
{
DataRow dr = tb.NewRow();
dr["Col1"] = DSEmp.Tables[k].Rows[0].ToString();
dr["Col1"] = DSEmp.Tables[k].Rows[1].ToString();
tb.Rows.Add(dr);
}
}
// Creates dataview of the table
DataView dv = new DataView(tb);
// Assigning Dataview to datagrid datasource
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
|
|
|
You achieve this using one adaptor and one dataset. In side dataset, u can store multiple tables. Use Fill method to fill each table.
Once you get all three tables in dataset, create a you custom table using Datatable and merge three tables. Then create dataview for that datatable. Use this dataview as datagrid datasource. Following code snap would help to understand…
// Sql connection..
SqlConnection con = new SqlConnection(connectionString);
// Create Data Adaptor
SqlDataAdapter DAEmp = new SqlDataAdapter();
// Sql command for selecting tables
SqlCommand Dcom = new SqlCommand();
Dcom.CommandText = @"Select * from table1"; // first table
Dcom.Connection = con;
DAEmp.SelectCommand = Dcom;
// create Data set
DataSet DSEmp = new DataSet();
DAEmp.Fill(DSEmp,"Table1"); // Filling first table
Dcom.CommandText = @"select * from table2"; // Second table
DAEmp.SelectCommand = Dcom;
DAEmp.Fill(DSEmp,"Table2"); // Filling second table
Dcom.CommandText = @"select * from table3"; // Third table
DAEmp.SelectCommand = Dcom;
DAEmp.Fill(DSEmp,"Table3"); // Filling third table
// Create table for merger all three tables
DataTable tb = new DataTable();
// Data column to add in table
DataColumn dc = new DataColumn("Col1",typeof(string));
DataColumn dc1 = new DataColumn("Col2",typeof(string));
tb.Columns.Add(dc);
tb.Columns.Add(dc1);
// Fetching all three table – to insert row in table
for ( int k = 0; k < DSEmp.Tables.Count; k++ )
{
// Fetching all row of each table – to insert row in table
for ( int i = 0; i < DSEmp.Tables[k].Rows.Count ; i++ )
{
DataRow dr = tb.NewRow();
dr["Col1"] = DSEmp.Tables[k].Rows[0].ToString();
dr["Col1"] = DSEmp.Tables[k].Rows[1].ToString();
tb.Rows.Add(dr);
}
}
// Creates dataview of the table
DataView dv = new DataView(tb);
// Assigning Dataview to datagrid datasource
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
|
|
|
|
|
|
|
|