wpfvb.netwpfdatagridwpf-4.0

How to convert DataGrid to dataTable


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


Solution

  • 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();