We will use one GridView and its sorting event, then we write some code in that event for making name ascending and descending.

Initial chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (gridviewsorting_demo).

Step 2

In Solution Explorer you get your empty website, then add two web forms and a SQL Server Database as in the following.

For Web Form:

Autocomplete_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it gridviewsorting_demo.aspx.

For SQL Server Database

gridviewsorting_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't Forget to make ID as IS Identity -- True)


table design

Design chamber

Step 4

Now open your gridviewsorting_demo.aspx file, where we create our design for GridView sorting.

Gridviewsorting_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table style="width:100%;">
  11. <tr>
  12. <td>
  13. </td>
  14. <td>
  15. </td>
  16. <td>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>
  21. </td>
  22. <td>
  23. <asp:GridView ID="GridView1" runat="server" AllowSorting="True"
  24. AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
  25. BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="id"
  26. ForeColor="Black" GridLines="Vertical" onsorting="GridView1_Sorting">
  27. <AlternatingRowStyle BackColor="#CCCCCC" />
  28. <Columns>
  29. <asp:TemplateField HeaderText="Id" SortExpression="id">
  30. <%--<EditItemTemplate>
  31. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
  32. </EditItemTemplate>--%>
  33. <ItemTemplate>
  34. <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
  35. </ItemTemplate>
  36. </asp:TemplateField>
  37. <asp:TemplateField HeaderText="First Name" SortExpression="fname">
  38. <EditItemTemplate>
  39. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("fname") %>'></asp:TextBox>
  40. </EditItemTemplate>
  41. <ItemTemplate>
  42. <asp:Label ID="Label2" runat="server" Text='<%# Bind("fname") %>'></asp:Label>
  43. </ItemTemplate>
  44. </asp:TemplateField>
  45. <asp:TemplateField HeaderText="Last Name" SortExpression="lname">
  46. <EditItemTemplate>
  47. <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
  48. </EditItemTemplate>
  49. <ItemTemplate>
  50. <asp:Label ID="Label3" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
  51. </ItemTemplate>
  52. </asp:TemplateField>
  53. <asp:TemplateField HeaderText="City" SortExpression="city">
  54. <EditItemTemplate>
  55. <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
  56. </EditItemTemplate>
  57. <ItemTemplate>
  58. <asp:Label ID="Label4" runat="server" Text='<%# Bind("city") %>'></asp:Label>
  59. </ItemTemplate>
  60. </asp:TemplateField>
  61. </Columns>
  62. <FooterStyle BackColor="#CCCCCC" />
  63. <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
  64. <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  65. <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
  66. <SortedAscendingCellStyle BackColor="#F1F1F1" />
  67. <SortedAscendingHeaderStyle BackColor="#808080" />
  68. <SortedDescendingCellStyle BackColor="#CAC9C9" />
  69. <SortedDescendingHeaderStyle BackColor="#383838" />
  70. </asp:GridView>
  71. </td>
  72. <td>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td>
  77. </td>
  78. <td>
  79. </td>
  80. <td>
  81. </td>
  82. </tr>
  83. </table>
  84. </div>
  85. </form>
  86. </body>
  87. </html>
Now for those of who are using the Template Field here, you need to embed a single thing into the code, a Sort Expression, whatever the field is for (like if it is the firstname, then write like sort expression=”firstname”). The following code displays it.

code

Your design looks like the following:

gridview

Code chamber

Step 5

Open your gridviewsorting_demo.aspx.cs and write some code for the application to make it work.

gridviewsorting_demo.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!Page.IsPostBack)
  14. {
  15. refreshdata();
  16. }
  17. }
  18. public void refreshdata()
  19. {
  20. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  21. SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
  22. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  23. DataTable dt = new DataTable();
  24. sda.Fill(dt);
  25. GridView1.DataSource = dt;
  26. GridView1.DataBind();
  27. ViewState["dirState"] = dt;
  28. ViewState["sortdr"] = "Asc";
  29. }
  30. protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
  31. {
  32. DataTable dtrslt= (DataTable)ViewState["dirState"];
  33. if (dtrslt.Rows.Count > 0)
  34. {
  35. if (Convert.ToString(ViewState["sortdr"]) == "Asc")
  36. {
  37. dtrslt.DefaultView.Sort = e.SortExpression + " Desc";
  38. ViewState["sortdr"] = "Desc";
  39. }
  40. else
  41. {
  42. dtrslt.DefaultView.Sort = e.SortExpression + " Asc";
  43. ViewState["sortdr"] = "Asc";
  44. }
  45. GridView1.DataSource = dtrslt;
  46. GridView1.DataBind();
  47. }
  48. }
  49. }
Output chamber
Output

stored data

I hope you liked this. Thank you for reading. Have a good day.