Example E2245
Visible to All Users

Grid View for ASP.NET Web Forms - How to sort data based on user preferences

This example demonstrates how to allow users to sort data based on their preferences in the Grid View control.

Sort Grid Data

In this example, the Grid View contains an unbound column that displays text boxes in data cells. Users can enter numbers in text boxes and sort grid data based on these numbers.

Files to Review

Documentation

More Examples

Example Code

Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxwgv" %> <%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxe" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="CategoryID" OnCustomUnboundColumnData="ASPxGridView1_CustomUnboundColumnData"> <Columns> <dxwgv:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="0"> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1"> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="Description" VisibleIndex="2"> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="LowerBound" VisibleIndex="3" UnboundType="Integer"> <DataItemTemplate> <dxe:ASPxSpinEdit ID="txtLB" runat="server" Width="60px" OnInit="txtLB_Init"> <SpinButtons ShowIncrementButtons="false" ShowLargeIncrementButtons="false"> </SpinButtons> </dxe:ASPxSpinEdit> </DataItemTemplate> </dxwgv:GridViewDataTextColumn> </Columns> </dxwgv:ASPxGridView> </div> <dxe:ASPxButton ID="ASPxButton1" runat="server" Text="Save" OnClick="ASPxButton1_Click"> </dxe:ASPxButton> </form> </body> </html>
Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Data.OleDb; using System.Collections.Generic; using DevExpress.Web; public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { if (Session["dataSource"] == null) Session["dataSource"] = GetDataSource(); ASPxGridView1.DataSource = Session["dataSource"]; ASPxGridView1.DataBind(); } protected void txtLB_Init(object sender, EventArgs e) { Dictionary<object, int> lowerBoundStorage = Session["lowerBoundStorage"] as Dictionary<object, int>; if (lowerBoundStorage != null) { ASPxSpinEdit editor = (ASPxSpinEdit)sender; GridViewDataItemTemplateContainer templateContainer = (GridViewDataItemTemplateContainer)editor.NamingContainer; object key = templateContainer.KeyValue; if (lowerBoundStorage.ContainsKey(key)) editor.Value = lowerBoundStorage[key]; } } protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridViewColumnDataEventArgs e) { if (e.Column.FieldName == "LowerBound") { Dictionary<object, int> lowerBoundStorage = Session["lowerBoundStorage"] as Dictionary<object, int>; if (lowerBoundStorage == null) { lowerBoundStorage = new Dictionary<object, int>(); Session["lowerBoundStorage"] = lowerBoundStorage; } object key = e.GetListSourceFieldValue(e.ListSourceRowIndex, "CategoryID"); if (lowerBoundStorage.ContainsKey(key)) e.Value = lowerBoundStorage[key]; else e.Value = 0; } } protected void ASPxButton1_Click(object sender, EventArgs e) { Dictionary<object, int> lowerBoundStorage = Session["lowerBoundStorage"] as Dictionary<object, int>; if (lowerBoundStorage == null) lowerBoundStorage = new Dictionary<object, int>(); int startIndex = ASPxGridView1.PageIndex * ASPxGridView1.SettingsPager.PageSize; int endIndex = Math.Min(ASPxGridView1.VisibleRowCount, startIndex + ASPxGridView1.SettingsPager.PageSize); for (int i = startIndex; i < endIndex; i++) { ASPxSpinEdit txtLowerBound = (ASPxSpinEdit)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["LowerBound"], "txtLB"); if(txtLowerBound.Text == "") return; int lowerBound = int.Parse(txtLowerBound.Text.Trim()); object key = ASPxGridView1.GetRowValues(i, "CategoryID"); if (!lowerBoundStorage.ContainsKey(key)) lowerBoundStorage.Add(key, lowerBound); else lowerBoundStorage[key] = lowerBound; } Session["lowerBoundStorage"] = lowerBoundStorage; } private DataTable GetDataSource() { DataTable dataTable; using (OleDbConnection connection = new OleDbConnection()) { connection.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", MapPath("~/App_Data/nwind.mdb")); dataTable = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(string.Empty, connection); adapter.SelectCommand.CommandText = "SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"; adapter.Fill(dataTable); } return dataTable; } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.