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

    Copy one datatable values to another datatable dynamically

    Hi,
    How to get columns and row values dynamically and add into new datatable dyanamically.
    ex : 1st datatable is filled with 3 columns and 2 row values. i need to get columns name and row values dynamically and assign it to new dt.

    My code :

    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    DataTable dt = new DataTable();
    var dc = (from DataColumn Dc in table.Columns select new { dcc = Dc.ColumnName, Dc.DataType }).ToList();
    foreach (var vv in dc)
    {
    dt.Columns.Add(vv.dcc);
    }
    foreach(var v1 in table.Rows)
    {
    dt.Rows.Add(v1);
    }

    unable to assign row values according to that column names which fetched dynamically from 1st datatable. pls help to get row values inside the final foreach and assign according to that columns. so that next time when i pass 1st datatable with 5 columns and 3 rows it will be easy to complete my requirement.
  • #768318
    If you are interest to transfer specific rows from DataTable to another Datable in c# then you can use ImportRow ()
    dttableNew  = dttableOld.Clone();  

    foreach (DataRow drtableOld in dttableOld.Rows)
    {
    if (/*put your Condition */)
    {
    dtTableNew.ImportRow(drtableOld);
    }
    }

    Code Snippet for Copy one datatable values to another datatable

    DataTable tablename1 = GetData();
    DataTable tablename2 = GetData();
    foreach (DataRow item in table2.Rows) tablename1.ImportRow(item); }


    Reference:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308909

    http://stackoverflow.com/questions/4020270/copy-rows-from-datatable-to-another-datatable-c-sharp

  • #768321
    Hi,

    For normal Datatable to Datatable Copying will be simple task like we can use the function likes,

    Data Copy

    DataTable dt_copy = new DataTable();
    dt.TableName = Callfunction();
    dt_copy = dt.Copy();


    Data Clone

    DataTable dt_clone = new DataTable();
    dt.TableName = CloneFunction();
    dt_clone = dt.Clone();


    For you Dynamic datatable, like you don't know about the datatable its values and sizes.
    So you can implement below pseudo code to get it done.

    DataTable dt=new DataTable(); // Consider this is your first Datatable
    dt = _ds.Tables[0];

    DataTable dt1 = ds1.Tables[0];//Second Datatable

    for (int i = 0; i < dt1.Rows.Count; i++)

    {

    dt.ImportRow(dt1.Rows[i]); // Now we are importing the First Datatable with the Second Datatable the Columns should be matched other than that rows was not a constraint

    }

    Thanks,
    Mani


Sign In to post your comments