WHY I AM NOT ABLE TO USE A SINGLE DATA TABLE TO LOAD 2 GRID CONTROLS??

mohan kumar

Total Post:10

Points:70
Posted by  mohan kumar
 2410  View(s)
Ratings:
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!!!!
  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: why i am not able to use a single data table to load 2 grid controls??

    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,

  1. mohan kumar

    Post:10

    Points:70
    Re: why i am not able to use a single data table to load 2 grid controls??

    Post is removed by the User.

  1. mohan kumar

    Post:10

    Points:70
    Re: why i am not able to use a single data table to load 2 grid controls??

    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

  1. James Smith

    Post:48

    Points:336
    Re: why i am not able to use a single data table to load 2 grid controls??

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

  1. Alex R

    Post:40

    Points:280
    Re: why i am not able to use a single data table to load 2 grid controls??

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!