How to convert DataGrid to dataTable

NorCode picture NorCode · Feb 17, 2014 · Viewed 26.1k times · Source

I want to copy all the datagrid records into datatable without using any loop. For Ex:

Dim dt as New DataTable
dt = Datagrid1.Items

But this is not Working and giving an error message.

My Development platform is Visual studio 2010 and language is WPF with vb.net 4.0

Answer

Rishap Gandhi picture Rishap Gandhi · Feb 28, 2014

This is the way to transfer all the records from DATAGRID to DATATABLE without using the LOOP.

VB:

Dim dt As New DataTable
dt = CType(DataGrid1.ItemsSource, DataView).ToTable

C#:

DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();