Home > DeveloperSection > Forums > why i am not able to use a single data table to load 2 grid controls??
mohan kumar

Total Post:10

Points:70
Posted on    December-05-2011 4:57 AM

 ASP.Net ASP.Net 
Ratings:


 5 Reply(s)
 2181  View(s)
Rate this:
Guys!! I am stuck up here!!! why i am not able to use a single data table to load 2 grid controls??

 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!!!!


Awadhendra Tiwari

Total Post:126

Points:882
Posted on    December-06-2011 10:09 AM

Hey Mohan Kumar,
You can not use same DataTable object to bind two grid views. You have to use two DataTable objects to bind record.

Thanks,

mohan kumar

Total Post:10

Points:70
Posted on    December-06-2011 7:15 PM

Post is removed by the User.

mohan kumar

Total Post:10

Points:70
Posted on    December-06-2011 7:16 PM

It is possible...

For 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

James Smith

Total Post:48

Points:336
Posted on    December-06-2011 11:34 PM

dt is not object. It's only reference variable which points a Data Table object.

Alex R
Alex R

Total Post:40

Points:280
Posted on    December-07-2011 1:13 AM

Hi 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.


Don't want to miss updates? Please click the below button!

Follow MindStick