How to implement sorting in GridView using c#?

Paging and Sorting in ASP.NET GridView

  1. Paging and Sorting in ASP.NET GridView.
  2. Step1: Create a table in the database.
  3. Step 2: Create a new ASP.NET web application and drag a GridView control in the Default.
  4. Step 3: Write the following in the page load event:

How do I sort GridView?

Use the Sort method to programmatically sort the GridView control using the specified sort expression and direction. The sort expression specifies the column or columns with which to sort. To sort multiple columns, create a sort expression that contains a comma-separated list of field names.

What is sort expression in GridView?

By default, the GridView control sorts a single column at a time. The sort expression simply contains the name of the field to sort. You can also sort multiple columns at a time by programmatically setting this property to a comma-separated list of field names.

How do I sort gridview columns in asp net vb net?

I will demonstrate one of the ways of performing sorting on gridview . Drag and drop gridview on your page. Set AllowSorting property to true , add OnSorting event to the gridview . Create data table that you want to assign to the GridView .

How to sort only displayed rows in GridView?


  • <asp:GridView ID=”gvCustomers” OnSorting=”OnSorting” runat=”server” AutoGenerateColumns=”false” CssClass=”gv”
  • AllowSorting=”True” CellPadding=”4″ DataKeyNames=”CustomerId”>
  • <Columns>
  • <asp:TemplateField
  • HeaderText=”Customer ID”
  • SortExpression=”CustomerId”>
  • <ItemTemplate>
  • <%#Eval (“CustomerId”)%>
  • </ItemTemplate>
  • How to get selected value from GridView?

    <asp:GridView runat =”Server” ID=”Gridview1″ AutoGenerateColumns=”False” DataSourceID=”SqlDataSource1″AutoGenerateSelectButton=”True” OnSelectedIndexChanged=”Gridview1_SelectedIndexChanged Width=”306px” Height=”137px”>

  • <Columns >
  • <asp:BoundField HeaderText =”Rowid” DataField =”Rowid”/>
  • <asp:BoundField HeaderText =”Name” DataField =”Name”/>
  • How to sort in datagridview?

    In general,the DataGridView control provides automatic sorting,so that user can manually sort any column in the control.

  • Here we can control whether a column can be sorted by setting the SortMode property of the DataGridViewColumn.
  • We can also programmatically sort a column.
  • How can I disable sorting in a datagridview?

    – Open the EditColumns window of the DataGridView control. – Select the column you want to make not sortable on the left side pane. – In the right side properties pane, select the Sort Mode property and select “Not Sortable” in that.