how to use datatable with SfDataPager

hi

I need to use Datatable with sfdatagrid but with using SfDataPager.

I saw that I can't but I need any method to use or convert the Datatable to be appropriate with sfdatagrid.

thanks


1 Reply

DM Dhanasekar Mohanraj Syncfusion Team December 8, 2022 02:07 PM UTC

Hi Mohamad,

By default, SfDataPager doesn’t accept DataTable as a Source. We have already documented this as a limitation as shown below,

However, you can achieve your requirement to use the DataTable by using ExpandoObject shown below,

ExpandoObject:

private DataTable dataTableCollection;

private ObservableCollection<dynamic> dynamicCollection;

public Form1()

{

    InitializeComponent();

    this.WindowState = FormWindowState.Maximized;

    //Gets the data for DataTable object.

    dataTableCollection = GetGridData();

 

    //Convert DataTable collection as Dyanamic collection.

    dynamicCollection = new ObservableCollection<dynamic>();

    foreach (System.Data.DataRow row in dataTableCollection.Rows)

    {

        dynamic dyn = new ExpandoObject();

        dynamicCollection.Add(dyn);

        foreach (DataColumn column in dataTableCollection.Columns)

        {

            var dic = (IDictionary<stringobject>)dyn;

            dic[column.ColumnName] = row[column];

        }

    }

 

    DynamicOrders = dynamicCollection;

    sfDataGrid1.AutoGenerateColumns = true;

    sfDataGrid1.DataSource = DynamicOrders;

}

 

 

private ObservableCollection<dynamic> _dynamicOrders;

 

/// <summary>

/// Gets or sets the dynamic orders.

/// </summary>

/// <value>The dynamic orders.</value>

public ObservableCollection<dynamic> DynamicOrders

{

    get

    {

        return _dynamicOrders;

    }

    set

    {

        _dynamicOrders = value;

    }

}

 

public DataTable DataTableCollection

{

    get { return dataTableCollection; }

    set { dataTableCollection = value; }

}


Here we have prepared the sample based on your scenario. Please have a look at this.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Regards,

Dhanasekar M.


Attachment: SfDatagridDemo__906b287f.zip

Loader.
Up arrow icon