CSharp Adding Indicators To Display Sorting Direction – ADO.Net Database

<%@ Page Language="C#" %>
<script runat="server">
    void deptView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            string imageUrl = (deptView.SortDirection==SortDirection.Ascending ?"Asc.gif" :"Desc.gif");
            for(int i=0; i<deptView.Columns.Count; i++)
            {
                string columnExpression = deptView.Columns[i].SortExpression;
                if (columnExpression != "" && columnExpression == deptView.SortExpression)
                {
                    Image img = new Image();
                    img.ImageUrl =imageUrl;
                    e.Row.Cells[i].Controls.Add(img);
                }
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Adding Indicators to display Sorting Direction</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:GridView ID="deptView"
                    AllowSorting="true"
                    runat="server"
                    AutoGenerateColumns="false"
                    DataSourceID="deptSource"
                    OnRowCreated="deptView_RowCreated">
        <columns>
          <asp:BoundField HeaderText="Department ID"
                          DataField="DepartmentID"
                          SortExpression="DepartmentID" />
          <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" />
          <asp:BoundField HeaderText="Group Name" DataField="GroupName" />
        </columns>
      </asp:GridView>
      <asp:SqlDataSource ID="deptSource"
                         Runat="server"
                         SelectCommandType="Text"
                         SelectCommand="Select DepartmentID, Name, GroupName, ModifiedDate from HumanResources.Department"
                         ConnectionString="<%$ConnectionStrings:AdventureWorks%>">
      </asp:SqlDataSource>
    </div>
  </form>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.