SqlConnection conn = new SqlConnection("Data Source=INNOVA13;uid=sa;pwd=innova;Initial Catalog=Senthil");
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadmyGrid1();
loadmyGrid2();
}
}
public void loadmyGrid1()
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select eid,ename,gender,age,qualification,address from emp",conn);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
public void loadmyGrid2()
{
dt.Clear();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select departmentid,deaprtmentname,studname from department", conn);
sda.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
conn.Close();
}
}
I want to use the "dt" object to load both the grid view controls simultanously with different data sources..Help me out guys...
If i use same data table object "dt" in both grid controls, i am not able to retrieve my desired output... Help me out guys!!!!
Anonymous User
07-Dec-2011Hi Mohan,
You can use dt.Reset() insted of dt.Clear() in your loadmyGrid2() method mentioned above.
As dt.Clear() only clears the record but not the schema, so when you try to fill that DataTable again with different schema then in that case you will have both of your schema in your datatable, but dt.Reset(), resets you DataTable to its original state and hence when you re-fill your DataTable with different schema then you will have schema and and records associated with your current table.
Hope, this will help you.
James Smith
06-Dec-2011mohan kumar
06-Dec-2011For first grid,i will be declaring DataTable dt=new DataTable( );
GridView1.DataSource=dt;
GridView1.DataBind();
and then for second grid, i have to use
dt=new dataTable( );
GridView2.DataSource=dt;
GridView2.DataBind( );
Actually my ultimate aim is to use the same data table object name "dt" which get's satisfied... :) :)
Thanks for replying me Awadhendra Tiwari
mohan kumar
06-Dec-2011Anonymous User
06-Dec-2011You can not use same DataTable object to bind two grid views. You have to use two DataTable objects to bind record.
Thanks,