Skip to content

Latest commit

 

History

History
76 lines (61 loc) · 2.63 KB

File metadata and controls

76 lines (61 loc) · 2.63 KB

How to Use SfDataPager with DataTable as ItemSource for WinForms DataGrid?

By default, In WinForms DataGrid (SfDataGrid), SfDataPager doesn’t accept DataTable as a source, and we have documented this as a limitation in below user guide documentation.

UG Link: Winforms DataGrid Paging Limitation

However, we can provide a workaround to achieve your requirement by converting the DataTable to an ExpandoObject. Then, you can set the ExpandoObject collection as the DataSource for SfDataPager, as demonstrated below:

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<string, object>)dyn;
           dic[column.ColumnName] = row[column];
       }
   }

   DynamicOrders = dynamicCollection;
   sfDataPager1.DataSource = DynamicOrders;
   sfDataPager1.PageSize = 10;
   sfDataGrid1.DataSource = sfDataPager1.PagedSource;
}

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; 
   }
}

Image Reference:

DataPager_Image.png

Take a moment to peruse the Winforms DataGrid - Paging documentation, to learn more about paging with examples.