Adding Indicators to display Sorting Direction : GridView « ADO.net Database « ASP.Net






Adding Indicators to display Sorting Direction

<%@ 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>

 








Related examples in the same category

1.Displaying data in database table with the GridView control.
2.The GridView control supports programmatic databinding
3.Selecting Data
4.Using Data Keys
5.Paging Through Data
6.Sortable GridView
7.Change row style for higher value
8.Highlight Rows
9.Numeric Format Strings
10.Time and Date Format Strings
11.GridView custom paging with ObjectDataSource
12.GridView format event
13.Turn off the GridLine
14.GridView selection style
15.GridView Summaries
16.Using the RowDeleted event to catch SQL errors (C#)
17.Using the RowDeleted event to catch SQL errors (VB)
18.Adding a delete link to the GridView
19.Checking for Update errors using the RowUpdated event (C#)
20.Checking for Update errors using the RowUpdated event (VB)
21.Turning off AutoGenerateColumns in the GridView control
22.Using the RowCreated Event to programmatically change the style