Articles in this section

How to use SfDataPager with DataTable in 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.

View sample in GitHub

Conclusion

I hope you enjoyed learning about how to use SfDataPager with DataTable in WinForms DataGrid.

You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied