Hello Reader ,
When adding a row to DataTable, there are two methods available in C# and VB.Net i.e.
DataTable.Rows.Add(DataRow) and DataTable.ImportRow(DataRow). Both do the same functionality, adding a row to DataTable but the main difference is that if you want to make a new row in table you can use Row.Add() but if you want to import row from another table you can use ImportRow().
So ImportRow is basically used for using another row from different table. For example -
DataTable dtFirst = new DataTable();
DataTable dtSecond = new DataTable();
DataRow drFirst = dtFirst.NewRow();
dtSecond.Rows.Add(drFirst);
// Above line will give you error already drFirst belongs to another DataTable
// in that case you can do like this
dtSecond.ImportRow(drFirst);
dtFirst.Rows.Add(drFirst);
// Above line is safe as drFirst Row belongs to dtFirst DataTable
// so no exception will raise.
0 Comment(s)