2

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

1 Answer 1

3

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

SPGridView All - Grouping, Paging, Filtering, Sorting

Creation of a SPGridview Webpart having Pagination, Sorting, Filtering Functionalities in Sharepoint Site in Step by Step Manner

4
  • Hi Arsalan, thanks for your answer, putting the code in you posted for the filter will only display the filter UI, but the filter functionality won't be there, I have only data table connected directly to spgridview, I don't have objectdatasource as stated in my question, but the articles you mentioned they all lead to solutions with ObjectDataSource, is it required for the SPGridView? Apr 11, 2014 at 1:43
  • You can use existing table as ObjectDataSource, since ODS only fires one of the Server-side methods to get the data sources.. You can use it easily.. Apr 11, 2014 at 9:54
  • OK, but is it required for the SPGridView to work? or I can bind it directly to it. Because I've tried the method you sent above for filtering, it shows the filter, but it's disabled, you can't really filter, and on sort it will give error on post back. Apr 11, 2014 at 13:46
  • Not sure if that is necessary.. but it seems like it is.. Apr 11, 2014 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.