Skip to main content

SPGridView, Sorting, Filtering, Linq... WebParts.......

·358 words·2 mins
Sebastiaan Brozius
Author
Sebastiaan Brozius

I came across some very nice articles when browsing the internet for some information on how to enable filtering and sorting an an SPGridView with a LinqDataSource as datasource. The most usefull one I found is this one from Johan Leino.

However, there are still some things to be done.

Since I am writing a WebPart, I have to put things in manually. Which also means that I have to remind myself to give everything an ID. If I don’ tdo that, filtering will fail, because the methods that retrieve the list of values in the columns won’t know where to write back to.

For the filtering, the FilteredFieldDataFormat should be: "{1} == \"{0}\"" Somehow, the property and value are sent value, property to the format-function :S

Next, when sorting AND filtering at the same time, we don’t want to lose the filter. To accomplish this, we have to add the followin code to the DataBound-event of the SPGridView:

if (string.IsNullOrEmpty(gridview.FilterFieldName) == false)
{
linqDataSource.Where = string.Format("{0} == \"{1}\"",gridview.FilterFieldName,gridview.FilterFieldValue);
}

If that is done, we possibly would also like to show a filter-icon on the column that is being filtered. Add this code to the RowDataBound-event of the SPGridView:

            if (sender == null || e.Row.RowType != DataControlRowType.Header)
            { return; }

            SPGridView grid = sender as SPGridView;

            if (String.IsNullOrEmpty(grid.FilterFieldName))
            { return; }

            // Show icon on filtered column
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                DataControlField field = grid.Columns[i];

                if (field.SortExpression == grid.FilterFieldName)
                {
                    Image filterIcon = new Image();
                    filterIcon.ImageUrl = &quot;/_layouts/images/filter.gif&quot;;
                    filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = &quot;2px&quot;;

                    // If we simply add the image to the header cell it will
                    // be placed in front of the title, which is not how it
                    // looks in standard SharePoint. We fix this by the code
                    // below.
                    Literal headerText = new Literal();
                    headerText.Text = field.HeaderText;

                    PlaceHolder panel = new PlaceHolder();
                    panel.Controls.Add(headerText);
                    panel.Controls.Add(filterIcon);

                    e.Row.Cells[i].Controls[0].Controls.Add(panel);

                    break;
                }
            }

Tada!! Now we have an SPGridView with sorting and filtering.

Now to find a way to do this with Parent-Child-relations and not havin the ID’s in the SPGridView, but the wanted info from the Child-table AND have sorting and filtering…

To be continued….