Below I showed my example where i try to add first table field values to secodn table. According to second table column name.Want to say , if first table FIELD_ID = second table column name then add.if any idea help please
DataTable dt = new DataTable();
dt.Columns.Add("Field_ID", typeof(Int32));
dt.Columns.Add("Field", typeof(String));
dt.Rows.Add(new object[] { 1, "Masin" });
dt.Rows.Add(new object[] { 2, "Tank" });
dt.Rows.Add(new object[] { 3, "Helicopter" });
DataTable dt2 = new DataTable();
dt2.Columns.Add("1", typeof(String));
dt2.Columns.Add("2", typeof(String));
Thanks in advance.
Vishal GilbilePosted Mar 29, 2013, 12:37 AM
you can try the following code. There are two ways to do the same.
Way 1:
DataRow dr = dt2.NewRow();
Way 2: The Better Approach
Hope that solves your problem.
With Regards,
Vishal Gilbile.
Posted Mar 29, 2013, 1:51 AM
Vishal Gilbine.
Sreejesh SPPosted Mar 28, 2013, 2:47 PM
private void button1_Click(object sender, EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add("Field_ID",typeof(Int32));
dt.Columns.Add("Field",typeof(string));
dt.Rows.Add(1,"Masin");
dt.Rows.Add(2,"Tank");
dt.Rows.Add(3,"Helicopter");
DataTable dt2=new DataTable();
for(int i=0;i
string column=Convert.ToString(dt.Rows[i]["Field_ID"]);
dt2.Columns.Add(column,typeof(String));
}
}
Please Try the above code this code defintly working,because this code work in my application
Thanks
Sreejesh
Posted Mar 28, 2013, 1:03 PM
but values from dt1 dont inserted to dt2 according to column names?
Thanks
Sreejesh SPPosted Mar 28, 2013, 11:51 AM
Please try below Code
Datatable dt=new Datatable();
dt.columns.Add("Field_ID",typeof(int32));
dt.columns.Add("Field",typeof(string));
dt.rows.add(1,"Masin");
dt.rows.add(2,"Tank");
dt.rows.add(3,"Helicopter");
Datatable dt2=new datatable();
for(int i=0:i
string column=convert.tostring(dt.rows[i]]["Field_ID"]);
dt2.column.Add(column,typeof(String));
}
Thanks
Sreejesh