We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Use DataTable as ItemSource for DataGrid

Thread ID:

Created:

Updated:

Platform:

Replies:

149900 Dec 13,2019 11:01 AM UTC Dec 16,2019 12:25 PM UTC UWP 1
loading
Tags: SfDataGrid
Frank Effenberger
Asked On December 13, 2019 11:01 AM UTC

Hi,

is it possible to use a DataTable as ItemSource for DataGrid?

XAML:
             <grid:SfDataGrid x:Name="dataGrid" AutoGenerateColumns="True"/>

C#:
            DataTable table = new DataTable();
            table.Columns.Add("Column1", typeof(string));
            table.Columns.Add("Column2", typeof(string));
            table.Columns.Add("Column3", typeof(string));
            table.Rows.Add("Entry1", "Entry2", "Entry3");
            table.Rows.Add("Entry4", "Entry5", "Entry6");
            table.Rows.Add("Entry7", "Entry8", "Entry9");
            dataGrid.ItemsSource = table.DefaultView;

this does not work for me.

Best Regards,
Frank

Mohanram Anbukkarasu [Syncfusion]
Replied On December 16, 2019 12:25 PM UTC

Hi Frank,   
  
Thank you for contacting Syncfusion support.

As of now, DataGrid doesn’t have support to bind DataTable to the ItemsSource in UWP platform. Microsoft provided DataTable support only from Fall Creators update but our UWP library was generated by targeting the version as Build:10240 in order to provide backward compatibility for all the UWP versions from initial. Now, we don’t have any immediate plan to upgrade our library to build with the latest SDK version and so unable to provide direct support to bind ItemsSource with DataTable.   
  
You can create your UWP application by specifying the Target and Min version as Fall Creators Update or later versions, so that you can get the DataTable API. And then use the below code to convert DataTable to the ObservableCollection of Dynamic object and bind this collection to the ItemsSource of DataGrid. It will not be required to create Model and this helper method will support you to use it for different columns. Try using the below code example and let us know if this helps you.   
  
Code example:    

public static ObservableCollection<dynamic> ToDynamic(this DataTable dt)   
{   
    var dynamicDt = new ObservableCollection<dynamic>();   
    foreach (System.Data.DataRow row in dt.Rows)   
    {   
        dynamic dyn = new ExpandoObject();   
        dynamicDt.Add(dyn);   
        //Converting the DataTable collcetion in Dynamic collection    
        foreach (System.Data.DataColumn column in dt.Columns)   
        {   
            var dic = (IDictionary<stringobject>)dyn;   
            dic[column.ColumnName] = row[column];   
        }   
    }   
    return dynamicDt;   
}   
   
dataGrid.ItemsSource = ToDynamic(table);   


Please let us know if you require further assistance from us.    
  
Regards,   
Mohanram A.   



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon