Category / Section
How to Use SfDataPager with DataTable as ItemSource for SfDataGrid ?
2 mins read
By default, SfDataPager doesn’t accept DataTable as a source, and we have documented this as a limitation below.
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:
Take a moment to peruse the Winforms DataGrid - Paging Documentation documentation, to learn more about Paging with examples.