Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to convert datarow array to datatable

I want to convert a DataRow array into DataTable ... What is the simplest way to do this?

like image 883
Developer404 Avatar asked Jan 23 '10 07:01

Developer404


1 Answers

For .Net Framework 3.5+

DataTable dt = new DataTable(); DataRow[] dr = dt.Select("Your string"); DataTable dt1 = dr.CopyToDataTable(); 

But if there is no rows in the array, it can cause the errors such as The source contains no DataRows. Therefore, if you decide to use this method CopyToDataTable(), you should check the array to know it has datarows or not.

if (dr.Length > 0)     DataTable dt1 = dr.CopyToDataTable(); 

Reference available at MSDN: DataTableExtensions.CopyToDataTable Method (IEnumerable)

like image 179
joe Avatar answered Oct 12 '22 11:10

joe