Gagan Marwah

Gagan Marwah

  • NA
  • 11
  • 5.7k

Data not sorting when paging

Sep 7 2017 1:09 PM

Hi I am trying to sort the data in ASC and DESC order. When I put a breakpoint, I see that the data is being sorted fine. The problem occurs when paging through the GridView. The data is not sorted anymore when I go to next page. Can someone please suggest what I need to fix or add to my code? Thanks is advance.

 ASPX Page:

  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"  ForeColor="#333333" GridLines="None" PageSize="20"   
  2.             OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"  OnSorting="GridView1_Sorting">  
  3.             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  4.             <Columns>  
  5.   
  6.   
  7.                 <%-- COMMENT ID --%>  
  8.                 <asp:TemplateField>  
  9.                     <ItemTemplate>  
  10.                         <asp:Label  Visible="false" ID="lblID" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>  
  11.   
  12.                         </asp:Label>  
  13.                     </ItemTemplate>  
  14.                     <EditItemTemplate>  
  15.                         <asp:Label ID="txtID" Visible="false" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>  
  16.   
  17.                         </asp:Label>  
  18.                     </EditItemTemplate>  
  19.                 </asp:TemplateField>  
  20.   
  21.                 <%-- COMMENT YEAR --%>  
  22.                 <asp:TemplateField HeaderText="Year">  
  23.                     <ItemTemplate>  
  24.                         <asp:Label ID="lblCommentYear" Visible="true" runat="server" Text='<%# Bind("METRIC_YEAR") %>'>  
  25.   
  26.                         </asp:Label>  
  27.                     </ItemTemplate>  
  28.                     <EditItemTemplate>  
  29.                         <asp:Label ID="txtCommentYear" runat="server" Text='<%# Bind("METRIC_YEAR") %>'>  
  30.   
  31.                         </asp:Label>  
  32.                     </EditItemTemplate>  
  33.                 </asp:TemplateField>  
  34.   
  35.                 <%-- COMMENT MONTH --%>  
  36.                 <asp:TemplateField HeaderText="Month">  
  37.                     <ItemTemplate>  
  38.                         <asp:Label ID="lblCommentMonth" Visible="true" runat="server" Text='<%# Bind("METRIC_MONTH_TXT") %>'>  
  39.   
  40.                         </asp:Label>  
  41.                     </ItemTemplate>  
  42.                     <EditItemTemplate>  
  43.                         <asp:Label ID="txtCommentMonth" runat="server" Text='<%# Bind("METRIC_MONTH_TXT") %>'>  
  44.   
  45.                         </asp:Label>  
  46.                     </EditItemTemplate>  
  47.                 </asp:TemplateField>  
  48.   
  49.                 <%-- COMMENT TAB --%>  
  50.                 <asp:TemplateField HeaderText="Tab">  
  51.                     <ItemTemplate>  
  52.                         <asp:Label ID="lblCommentTab" Visible="true" runat="server" Text='<%# Bind("TAB_NAME") %>'>  
  53.   
  54.                         </asp:Label>  
  55.                     </ItemTemplate>  
  56.                     <EditItemTemplate>  
  57.                         <asp:Label ID="txtCommentTab" runat="server" Text='<%# Bind("TAB_NAME") %>'>  
  58.   
  59.                         </asp:Label>  
  60.                     </EditItemTemplate>  
  61.                 </asp:TemplateField>  
  62.   
  63.                 <%-- COMMENT SECTION --%>  
  64.                 <asp:TemplateField HeaderText="Section">  
  65.                     <ItemTemplate>  
  66.                         <asp:Label ID="lblCommentSection" Visible="true" runat="server" Text='<%# Bind("SECTION_NAME") %>'>  
  67.   
  68.                         </asp:Label>  
  69.                     </ItemTemplate>  
  70.                     <EditItemTemplate>  
  71.                         <asp:Label ID="txtCommentSection" runat="server" Text='<%# Bind("SECTION_NAME") %>'>  
  72.   
  73.                         </asp:Label>  
  74.                     </EditItemTemplate>  
  75.                 </asp:TemplateField>  
  76.   
  77.                 <%-- COMMENT ORDER --%>  
  78.                 <asp:TemplateField HeaderText="Order" SortExpression="COMMENT_ORDER">  
  79.                     <ItemTemplate>  
  80.                         <asp:Label ID="lblCommentOrder" runat="server" Text='<%# Bind("COMMENT_ORDER") %>'>  
  81.   
  82.                         </asp:Label>  
  83.                     </ItemTemplate>  
  84.                     <EditItemTemplate>  
  85.                         <asp:TextBox ID="txtCommentOrder"  Width="20px" runat="server" Text='<%# Bind("COMMENT_ORDER") %>'></asp:TextBox>  
  86.                         <asp:RequiredFieldValidator ID="rfvUpdateCommentOrder" ValidationGroup="UPDATE" runat="server"   
  87.                         ErrorMessage="Comment order is required." ForeColor="Brown"  Display="Dynamic" ControlToValidate="txtCommentOrder"></asp:RequiredFieldValidator>  
  88.                     </EditItemTemplate>  
  89.                 </asp:TemplateField>  
  90.                   
  91.                 <%-- COMMENT TEXT --%>  
  92.                 <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="Comments">  
  93.                     <ItemTemplate>  
  94.                         <asp:Label ID="lblComment" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'>  
  95.   
  96.                         </asp:Label>  
  97.                     </ItemTemplate>  
  98.                     <EditItemTemplate>  
  99.                         <asp:TextBox ID="txtEditComment"  TextMode="MultiLine" Width="400px" Height="30px" MaxLength="300" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'></asp:TextBox>  
  100.                         <asp:RequiredFieldValidator ID="rfvUpdateCommentText" ValidationGroup="UPDATE" runat="server"   
  101.                         ErrorMessage="Please enter a comment" ForeColor="brown" ControlToValidate="txtEditComment"></asp:RequiredFieldValidator>  
  102.                     </EditItemTemplate>  
  103.                 </asp:TemplateField>  
  104.                 <asp:TemplateField>  
  105.                     <ItemTemplate>  
  106.                         <asp:LinkButton ID ="lnkEdit" runat="server" CommandName="Edit" Text ="Edit"></asp:LinkButton>  
  107.                         <asp:LinkButton OnClientClick="return confirm('Are you sure you want to delete this comment?');" ID ="lnkDelete" runat="server" CommandName="Delete" Text ="Delete"></asp:LinkButton>  
  108.                     </ItemTemplate>  
  109.                     <EditItemTemplate>     
  110.                        <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>     
  111.                        <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>     
  112.                     </EditItemTemplate>  
  113.                 </asp:TemplateField>  
  114.                  
  115.             </Columns>  
  116.             <EditRowStyle BackColor="#999999" />  
  117.             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  118.             <HeaderStyle  HorizontalAlign="Left" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  119.             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  120.             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  121.             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  122.             <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  123.             <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  124.             <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  125.             <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  126.         </asp:GridView>  
 
 
 ASPX.CS Page:
  1. namespace ChngMgmt_Comments_and_Legends  
  2. {  
  3.     public partial class Comments : System.Web.UI.Page  
  4.     {  
  5.         private static readonly ILog logger = LogManager.GetLogger("LoggerName");  
  6.         protected void Page_Load(object sender, EventArgs e)  
  7.         {  
  8.            if(!IsPostBack)  
  9.             {  
  10.                GridView1.DataSource = RefreshGrid();  
  11.                GridView1.DataBind();  
  12.             }  
  13.         }  
  14.           
  15.   
  16.         public DataTable RefreshGrid()  
  17.         {  
  18.             //Gets all the data in grid view for the following columns  
  19.             String queryStr= "SELECT BRD_COMMENT_ID, METRIC_YEAR, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT FROM CHNGMETRICS.VW_BRD_COMMENT WHERE 1=1";  
  20.   
  21.             //If Year drop down is not null the data gets filtered by year  
  22.             if (DropDownYear.SelectedValue != "")  
  23.             {  
  24.                 queryStr += " AND METRIC_YEAR = :METRIC_YEAR";  
  25.                
  26.             }  
  27.             //If Month drop down in not null the data gets filtered by month.  
  28.             if (DropDownMonth.SelectedValue != "")  
  29.             {  
  30.                 queryStr += " AND METRIC_MONTH = :METRIC_MONTH";  
  31.   
  32.             }  
  33.             //if section drop down is not null the data gets filtered by section name  
  34.             if (DropDownSectionName.SelectedValue != "")  
  35.             {  
  36.                 queryStr += " AND SECTION_NAME = :SECTION_NAME";  
  37.   
  38.             }  
  39.             //if TAB drop down is not null the data gets filtered by TAB Name  
  40.             if (DropDownTabName.SelectedValue != "")  
  41.             {  
  42.                 queryStr += " AND TAB_NAME = :TAB_NAME";  
  43.   
  44.             }  
  45.   
  46.             //Added parameters for Year,Month,Section,Tab drop downs  
  47.             OracleCommand command = new OracleCommand(queryStr);  
  48.             if (DropDownYear.SelectedValue != "")  
  49.             {  
  50.                 command.Parameters.AddWithValue("METRIC_YEAR",DropDownYear.SelectedValue);  
  51.   
  52.             }  
  53.             if (DropDownMonth.SelectedValue != "")  
  54.             {  
  55.                 command.Parameters.AddWithValue("METRIC_MONTH", DropDownMonth.SelectedValue);  
  56.   
  57.             }  
  58.             if (DropDownSectionName.SelectedValue != "")  
  59.             {  
  60.                 command.Parameters.AddWithValue("SECTION_NAME", DropDownSectionName.SelectedValue);  
  61.   
  62.             }  
  63.             if (DropDownTabName.SelectedValue != "")  
  64.             {  
  65.                 command.Parameters.AddWithValue("TAB_NAME", DropDownTabName.SelectedValue);  
  66.   
  67.             }  
  68.   
  69.             DataTable GridComments = Utility.GetData(command);  
  70.             return GridComments;  
  71.   
  72.   
  73.         }  
  74.         //Drop Downs are auto post back so each time user selects a selection the Grid View needs to refresh  
  75.         protected void DropDownYear_SelectedIndexChanged(object sender, EventArgs e)  
  76.         {  
  77.             RefreshGrid();  
  78.         }  
  79.   
  80.         protected void DropDownMonth_SelectedIndexChanged(object sender, EventArgs e)  
  81.         {  
  82.             RefreshGrid();  
  83.         }  
  84.         protected void DropDownSectionName_SelectedIndexChanged(object sender, EventArgs e)  
  85.         {  
  86.             RefreshGrid();  
  87.         }  
  88.   
  89.         protected void DropDownTabName_SelectedIndexChanged(object sender, EventArgs e)  
  90.         {  
  91.             RefreshGrid();  
  92.         }  
  93.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  94.         {  
  95.             GridView1.EditIndex = e.NewEditIndex;  
  96.             RefreshGrid();  
  97.         }  
  98.         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  99.         {  
  100.             GridView1.PageIndex = e.NewPageIndex;  
  101.             DataTable dt = RefreshGrid();  
  102.             if (ViewState["Sort"] != null)  
  103.             {  
  104.                 dt.DefaultView.Sort = (string)ViewState["Sort"];  
  105.             }  
  106.             GridView1.DataSource = dt;  
  107.             GridView1.DataBind();  
  108.         }  
  109.   
  110.         // Grid View updating is enabled so users can edit Comments and order  
  111.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  112.         {  
  113.             logger.Debug("Debugging update statement");  
  114.             logger.Error("Catching error(s) while updating data");  
  115.             if (Page.IsValid)  
  116.             {  
  117.                 Label id = GridView1.Rows[e.RowIndex].FindControl("txtID"as Label;  
  118.                 TextBox comment = GridView1.Rows[e.RowIndex].FindControl("txtEditComment"as TextBox;  
  119.                 TextBox order = GridView1.Rows[e.RowIndex].FindControl("txtCommentOrder"as TextBox;  
  120.                 var updateQuery = ("UPDATE CHNGMETRICS.VW_BRD_COMMENT SET COMMENT_ORDER =  :COMMENTORDER , COMMENT_TEXT = :COMMENTTEXT WHERE BRD_COMMENT_ID = :BRDCOMMENTID ");  
  121.                 OracleCommand command = new OracleCommand(updateQuery);  
  122.                   
  123.                 //Add PARAMETERS FOR UPDATE STATEMENT  
  124.                 command.Parameters.Add(new OracleParameter("COMMENTORDER", order.Text));  
  125.                 command.Parameters.Add(new OracleParameter("COMMENTTEXT", comment.Text));  
  126.                 command.Parameters.Add(new OracleParameter("BRDCOMMENTID", Convert.ToInt32(id.Text)));  
  127.                 GridView1.EditIndex = -1;  
  128.                 Utility.Exec(command);  
  129.                 RefreshGrid();  
  130.                 //Year  
  131.                 DropDownYear.Items.Clear();  
  132.                 DropDownYear.DataBind();  
  133.                 DropDownYear.ClearSelection();  
  134.                 DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  135.                 DropDownYear.SelectedIndex = -1;  
  136.                 //Month  
  137.                 DropDownMonth.Items.Clear();  
  138.                 DropDownMonth.DataBind();  
  139.                 DropDownMonth.ClearSelection();  
  140.                 DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  141.                 DropDownMonth.SelectedIndex = -1;  
  142.                 //Tab  
  143.                 DropDownTabName.Items.Clear();  
  144.                 DropDownTabName.DataBind();  
  145.                 DropDownTabName.ClearSelection();  
  146.                 DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  147.                 DropDownTabName.SelectedIndex = -1;  
  148.                 //Section  
  149.                 DropDownSectionName.Items.Clear();  
  150.                 DropDownSectionName.DataBind();  
  151.                 DropDownSectionName.ClearSelection();  
  152.                 DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  153.                 DropDownSectionName.SelectedIndex = -1;  
  154.             }  
  155.               
  156.         }  
  157.   
  158.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  159.         {  
  160.             GridView1.EditIndex = -1;  
  161.             RefreshGrid();  
  162.         }  
  163.           
  164.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  165.         {  
  166.             logger.Debug("Debugging delete statement");  
  167.             logger.Error("Catching error(s) while deleting data");  
  168.             Label id = GridView1.Rows[e.RowIndex].FindControl("lblID"as Label;  
  169.             var deleteQuery = ("DELETE FROM CHNGMETRICS.VW_BRD_COMMENT WHERE BRD_COMMENT_ID = :BRDCOMMENTID ");  
  170.             OracleCommand command = new OracleCommand(deleteQuery);  
  171.             //Add PARAMETERS FOR DELETE STATEMENT  
  172.             command.Parameters.Add(new OracleParameter("BRDCOMMENTID", Convert.ToInt32(id.Text)));  
  173.             Utility.Exec(command);  
  174.             RefreshGrid();  
  175.             //Year  
  176.             DropDownYear.Items.Clear();  
  177.             DropDownYear.DataBind();  
  178.             DropDownYear.ClearSelection();  
  179.             DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  180.             DropDownYear.SelectedIndex = -1;  
  181.             //Month  
  182.             DropDownMonth.Items.Clear();  
  183.             DropDownMonth.DataBind();  
  184.             DropDownMonth.ClearSelection();  
  185.             DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  186.             DropDownMonth.SelectedIndex = -1;  
  187.             //Tab  
  188.             DropDownTabName.Items.Clear();  
  189.             DropDownTabName.DataBind();  
  190.             DropDownTabName.ClearSelection();  
  191.             DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  192.             DropDownTabName.SelectedIndex = -1;  
  193.             //Section  
  194.             DropDownSectionName.Items.Clear();  
  195.             DropDownSectionName.DataBind();  
  196.             DropDownSectionName.ClearSelection();  
  197.             DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  198.             DropDownSectionName.SelectedIndex = -1;  
  199.   
  200.         }  
  201.         protected void btnAdd_Click(object sender, EventArgs e)  
  202.         {  
  203.             logger.Debug("Debugging insert statement");  
  204.             logger.Error("Catching error(s) while adding data");  
  205.   
  206.             int month;  
  207.             if (drpDownMonth.Text == "January")  
  208.             {  
  209.                 month = 01;  
  210.             }  
  211.             else if (drpDownMonth.Text == "February")  
  212.             {  
  213.                 month = 02;  
  214.             }  
  215.             else if (drpDownMonth.Text == "March")  
  216.             {  
  217.                 month = 03;  
  218.             }  
  219.             else if (drpDownMonth.Text == "April")  
  220.             {  
  221.                 month = 04;  
  222.             }  
  223.             else if (drpDownMonth.Text == "May")  
  224.             {  
  225.                 month = 05;  
  226.             }  
  227.             else if (drpDownMonth.Text == "June")  
  228.             {  
  229.                 month = 06;  
  230.             }  
  231.             else if (drpDownMonth.Text == "July")  
  232.             {  
  233.                 month = 07;  
  234.             }  
  235.             else if (drpDownMonth.Text == "August")  
  236.             {  
  237.                 month = 08;  
  238.             }  
  239.             else if (drpDownMonth.Text == "September")  
  240.             {  
  241.                 month = 09;  
  242.             }  
  243.             else if (drpDownMonth.Text == "October")  
  244.             {  
  245.                 month = 10;  
  246.             }  
  247.             else if (drpDownMonth.Text == "November")  
  248.             {  
  249.                 month = 11;  
  250.             }  
  251.             else { month = 12;}  
  252.   
  253.   
  254.             //var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT VALUES ('" + "', '" + "', '" + "', '" + "', '" +drpDownYear.Text+ "', '" +month+ "', '" +drpDownMonth.Text+ "', '" +drpDownTabName.Text+ "', '" +drpDownSectionName.Text+ "', '" +drpDownCommentOrder.Text+ "', '" +txtComment.Text+ "')");  
  255.             var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");  
  256.             OracleCommand command = new OracleCommand(insertQuery);  
  257.   
  258.             //ADD PARAMETERS FOR INSERT STATEMENT  
  259.             command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));  
  260.             command.Parameters.Add(new OracleParameter("MONTH_NUM", month));  
  261.             command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));  
  262.             command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));  
  263.             command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));  
  264.             command.Parameters.Add(new OracleParameter("COMMENT_ORDER", txtCmntOrder.Text));  
  265.             command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));  
  266.   
  267.   
  268.             Utility.Exec(command);  
  269.             RefreshGrid();  
  270.   
  271.             //Year  
  272.             DropDownYear.Items.Clear();  
  273.             DropDownYear.DataBind();  
  274.             DropDownYear.ClearSelection();  
  275.             DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  276.             DropDownYear.SelectedIndex = -1;  
  277.             //Month  
  278.             DropDownMonth.Items.Clear();  
  279.             DropDownMonth.DataBind();  
  280.             DropDownMonth.ClearSelection();  
  281.             DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  282.             DropDownMonth.SelectedIndex = -1;  
  283.             //Tab  
  284.             DropDownTabName.Items.Clear();  
  285.             DropDownTabName.DataBind();  
  286.             DropDownTabName.ClearSelection();  
  287.             DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  288.             DropDownTabName.SelectedIndex = -1;  
  289.             //Section  
  290.             DropDownSectionName.Items.Clear();  
  291.             DropDownSectionName.DataBind();  
  292.             DropDownSectionName.ClearSelection();  
  293.             DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  294.             DropDownSectionName.SelectedIndex = -1;  
  295.               
  296.             txtComment.Text = String.Empty;  
  297.             txtCmntOrder.Text = String.Empty;  
  298.             drpDownYear.Text = "-1";  
  299.             drpDownMonth.Text = "-1";  
  300.             drpDownTabName.Text = "-1";  
  301.             drpDownSectionName.Items.Clear();  
  302.             drpDownSectionName.Items.Insert(0, new ListItem("Select section name""-1"));  
  303.             drpDownSectionName.Items.Add(new ListItem("Performance""Performance"));  
  304.             drpDownSectionName.Items.Add(new ListItem("Compliance""Compliance"));  
  305.             drpDownSectionName.Items.Add(new ListItem("Main""Main"));  
  306.             drpDownSectionName.Text = "-1";  
  307.             Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts","<script>alert('Your comment was successfully added.');</script>");  
  308.         }  
  309.   
  310.         protected void drpDownTabName_SelectedIndexChanged(object sender, EventArgs e)  
  311.         {  
  312.             drpDownSectionName.Items.Clear();  
  313.   
  314.             if (drpDownTabName.SelectedItem.Text == "Watch List")  
  315.             {  
  316.                 drpDownSectionName.Items.Add(new ListItem("Performance""Performance"));  
  317.                 drpDownSectionName.Items.Add(new ListItem("Compliance""Compliance"));                 
  318.             }  
  319.             else if ((drpDownTabName.SelectedValue == "General Stats") || (drpDownTabName.SelectedValue == "Planning") || (drpDownTabName.SelectedValue == "CAB"))  
  320.             {  
  321.                 drpDownSectionName.Items.Add(new ListItem("Main""Main"));  
  322.             }  
  323.         }  
  324.   
  325.         //protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
  326.         //{  
  327.   
  328.         //}  
  329.   
  330.         protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
  331.         {  
  332.             DataTable dt = RefreshGrid();  
  333.             //Sort the data.  
  334.             dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);  
  335.             GridView1.DataSource = dt.DefaultView;  
  336.             GridView1.DataBind();  
  337.   
  338.         }  
  339.   
  340.         private string GetSortDirection(string column)  
  341.         {  
  342.   
  343.             // By default, set the sort direction to ascending.  
  344.             string sortDirection = "ASC";  
  345.   
  346.             // Retrieve the last column that was sorted.  
  347.             string sortExpression = ViewState["SortExpression"as string;  
  348.   
  349.             if (sortExpression != null)  
  350.             {  
  351.                 // Check if the same column is being sorted.  
  352.                 // Otherwise, the default value can be returned.  
  353.                 if (sortExpression == column)  
  354.                 {  
  355.                     string lastDirection = ViewState["SortDirection"as string;  
  356.                     if ((lastDirection != null) && (lastDirection == "ASC"))  
  357.                     {  
  358.                         sortDirection = "DESC";  
  359.                     }  
  360.                 }  
  361.             }  
  362.   
  363.             // Save new values in ViewState.  
  364.             ViewState["SortDirection"] = sortDirection;  
  365.             ViewState["SortExpression"] = column;  
  366.   
  367.             return sortDirection;  
  368.         }  
  369.     }  
  370. }  
 

Answers (1)