User288213138 posted
Hi jsshivalik,
jsshivalik
In a dataset i have 1 Datatable dt1 . In this dt1 i want to add few more columns . from another datatable which has only 1 record with 5 fields. i want to append these 5 columns to dt1 with Values.
According to your description, I couldn’t understand your requirement clearly.
Is there any relationship between your dt1 and your another datatable?
Do the fields in dt1 match the fields in another datatable?
if not, you can refer to below code:
In the DataTable, I have defined six fields, where f1~f5 is for DataTable1. Then I add the fields to the new row by iterating through DataTable1, and finally add the new row to the DataTable.
The code:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("CustomerId")});
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[5] { new DataColumn("field1"), new DataColumn("field2") , new DataColumn("field3") , new DataColumn("field4") , new DataColumn("field5") });
dt1.Rows.Add("f1","f2","f3","f4","f5");
dt.Columns.AddRange(new DataColumn[5] { new DataColumn("f1"), new DataColumn("f2"), new DataColumn("f3"), new DataColumn("f4"), new DataColumn("f5") });
foreach (DataRow dr in dt1.Rows)
{
DataRow destRow = dt.NewRow();
destRow["f1"] = dr["field1"];
destRow["f2"] = dr["field2"];
destRow["f3"] = dr["field3"];
destRow["f4"] = dr["field4"];
destRow["f5"] = dr["field5"];
dt.Rows.Add(destRow);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
The result:

Best regards,
Sam