You must Sign In to post a response.
  • Category: .NET

    Bind 2 datatables in grid view

    how to bind 2 datatables to datagridview.
    I tried it like
    Dim dr, dr1 As SqlDataReader
    Dim dt, dt1 As New DataTable
    Dim ds As New DataSet
    dr = cmd.ExecuteReader()
    dt.Load(dr)
    dr1 = cmd1.ExecuteReader()
    dt1.Load(dr1)
    ds.Tables.Add(dt)
    ds.Tables.Add(dt1)
    DataGridView1.DataSource = ds
    but i didnt got output.. databind() in not in vb.net
  • #725293
    Use this code for Binding two data tables in single gridview
    DataTable datatable1;
    DataTable datatable2;
    foreach (DataRow datatable2.Rows)
    {
    DataRow Newrow = datatable1.NewRow();
    Newrow["Col1"] = row["Col1"];
    Newrow["Col2"] = row["Col2"];
    datatable1.Rows.Add(Newrow);
    Newrow.AcceptChanges();
    }
    For binding two datatable in C # use this link http://www.itdeveloperzone.com/2012/04/merge-2-datatables-in-c.html

  • #725311
    Hi,

    The way you are merging the datatable is wrong. Please use below logic.

    Create another dataTable with the name dtMerge

    dtMerge = dt.Copy();
    dtMerge.Merge(dt1,true);


    Now you may bind the gridview like below,

    DataGridView1.DataSource = dtMerge;
    DataGridView1.DataBind();


    Regards,
    Asheej T K

  • #725315
    Hi,

    Use below sample code while binding.

    DataTable dt= // table1 data;
    DataTable dt1= // table2 data;

    dt.Merge(dt1);

    GV.DataSource=dt;
    GV.DataBind();


    Hope this will resolve your problem...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #725369
    Hi,

    try like this option dt.Merge, if same column name present in the datatable1 then it merge data with existing column else it create new column and assign it.


    Dim dr, dr1 As SqlDataReader
    Dim dt, dt1 As New DataTable
    Dim ds As New DataSet
    dr = cmd.ExecuteReader()
    dt.Load(dr)
    dr1 = cmd1.ExecuteReader()
    dt1.Load(dr1)
    ds.Tables.Add(dt)
    ds.Tables.Add(dt1)
    dt.Merge(dt1)
    DataGridView1.DataSource = dt

    Regards
    N.Ravindran
    Your Hard work never fails


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.