This walkthrough shows you how to apply CustomSort in WebGrid.
During this walkthrough, you will learn how to do the following :
- Bind WebGrid to AccessDataSource
- Create a CustomSort in WebGrid's CustomSort server side event.
- Column's CustomerID will be sorted base on Column's ContactName
Prerequisites
In order to complete this walkthrough, you will need the following:
- Access to the Microsoft Access Northwind database.
- Visual Studio 2005 Application.
Step-By-Step Instructions
To create new web application and set CustomSort server side event.
- Bind WebGrid to AccessDataSource.
- Enable AllowEdit.
- Set Automatic Sort to false. You can find this property in WebGrid.NET Designer >> Advanced tab >> RootTable >> See all properties.
- In the CustomSort server side event of the WebGrid.
C# Copy Code WebGridGroup sortCol; sortCol = e.SortColumns.GetNamedItem("ContactName"); WebGridGroup oldSortCol; oldSortCol = e.SortColumns.GetNamedItem("CustomerID"); if (oldSortCol != null) { sortCol = new WebGridGroup(); sortCol.Table = oldSortCol.Table; sortCol.ColumnMember = "ContactName"; if (e.SortColumns.GetNamedItem("CustomerID").SortOrder == SortOrder.Ascending) { sortCol.SortOrder = SortOrder.Ascending; } else if (e.SortColumns.GetNamedItem("CustomerID").SortOrder == SortOrder.Descending) { sortCol.SortOrder = SortOrder.Descending; } e.SortColumns.Remove(oldSortCol); e.SortColumns.Add(sortCol); }
- Please note to "using ISNet.WebUI.WebGrid"