The difference between rows.add and importRow

When adding a row to datatable in vb.net, what is the difference between rows.add and importRow?

Dim dt As DataTable Dim dr As DataRow 'Add row this way... dt.rows.add(dr) 'or this way. dt.importRow(dr) 
+8
datatable
source share
2 answers

both perform the same functionality, adding a row to the datatable, but the main difference is

  DataTable dt1=new DataTable(); DataRow dr1=dt1.NewRow(); DataTable dt2=new DataTable(); dt2.Rows.Add(dr1); // will give you error already dr1 belongs to another datatable in that //case you can do like this dt2.ImportRow(dr1); // safe dt1.Rows.Add(dr1); // safe as dr1 Row belongs to DataTable1 so no exception raise 

Hope this will give you an idea.

+10
source share

The suggested answer is incorrect. The MSDN article below shows that dt2.ImportRow (dr1) will NOT import dr1. This is because it is in a disconnected state. Also dt2 has no circuit.

http://msdn.microsoft.com/en-us/library/system.data.datatable.importrow.aspx

+2
source share

All Articles