SPGridView Filtering & Sorting With DataTable
-
10-12-2019 - |
Question
I need a help filtering and sorting SPGridView based on DataTable, from the articles I've seen online, they use ObjectDataSource and so on, but I have the Grid binded directly to DataTable, is there any tutorial on how to implement the filtering or the sorting in this case? I've tried a lot and now working for me.
Thanks
Solution
Filtering
oGrid.AllowFiltering = true;
oGrid.FilterDataFields = "Title"; //tells the SPGridView what columns we want to be able to filter on.
oGrid.FilteredDataSourcePropertyName = "FilterExpression";
oGrid.FilteredDataSourcePropertyFormat = "{1} like '{0}'"; //property provides the format for our filter expression in a SQL-like syntax.
Sorting
oGrid.AllowSorting = true;
oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);
void oGrid_Sorting(object sender, GridViewSortEventArgs e)
{
// Call bind datatable function
BindData();
}
// Also make sure when you are adding the columns you should define SortExpression
BoundField colTitle = new BoundField();
colTitle.DataField = "Title";
colTitle.HeaderText = "Title";
colTitle.SortExpression = "Title";
this.oGrid.Columns.Add(colTitle);
References
SP GridView Filtering in share point 2010
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange