Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot sort gridview in des order, neither can sort on another column?
    primarykey
    data
    text
    <p>I have this grid view, it has 2 problems.</p> <ol> <li>if I sort it by clicking on a column, and then I click again, its not sorted in des order.</li> <li><p>If once I sort with a <code>col A</code> and then click on any other column its not sorted again?</p> <pre><code> &lt;asp:GridView ID="grdReport" runat="server" AutoGenerateColumns="False" DataKeyNames="CustCode" ShowFooter="True" EmptyDataText="No record found" PageSize="50" CssClass="mGrid" onrowdatabound="grdReport_RowDataBound" AllowSorting="True" onsorting="grdReport_Sorting"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText="Select"&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID="chkSelect" runat="server"/&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField Visible="false"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblCustCodes" runat="server" Text='&lt;%# Eval("CustCode") %&gt;' CssClass="grdCustName"&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Customer" SortExpression="Customer"&gt; &lt;ItemTemplate&gt; &lt;asp:HyperLink Target="_blank" Text='&lt;%# Eval("CustomerName") %&gt;' runat="server" ID="hplNavigate"&gt; &lt;/asp:HyperLink&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField="QTY" HeaderText="Booked Qty" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" SortExpression="QTY"&gt; &lt;FooterStyle HorizontalAlign="Right" /&gt; &lt;ItemStyle HorizontalAlign="Right"&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;asp:BoundField DataField="Volume" HeaderText="Booked Amt" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" SortExpression="Volume"&gt; &lt;FooterStyle HorizontalAlign="Right" /&gt; &lt;ItemStyle HorizontalAlign="Right"&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;/asp:BoundField&gt; &lt;asp:BoundField DataField="FirstBill" HeaderText="First Bill" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left" SortExpression="FirstBill"&gt; &lt;FooterStyle HorizontalAlign="Left" /&gt; &lt;ItemStyle HorizontalAlign="Left"&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;/Columns&gt; &lt;FooterStyle BackColor="Aqua" Font-Bold="true" ForeColor="BlueViolet"/&gt; </code></pre> <p></p></li> </ol> <p>The code behind for sorting is </p> <pre><code> switch (e.SortExpression) { case "Customer": if (e.SortDirection == SortDirection.Ascending) { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;string&gt;("CustomerName") select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } else { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;string&gt;("CustomerName") descending select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } break; case "QTY": if (e.SortDirection == SortDirection.Ascending) { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;int&gt;("Qty") select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } else { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;int&gt;("Qty") descending select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } break; case "Volume": if (e.SortDirection == SortDirection.Ascending) { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;float&gt;("Volume") select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } else { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;float&gt;("Volume") descending select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } break; case "FirstBill": if (e.SortDirection == SortDirection.Ascending) { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;DateTime&gt;("FirstBill") select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } else { var result = from table in Ob.DataTableOther.AsEnumerable() orderby table.Field&lt;DateTime&gt;("FirstBill") descending select table; var dv = result.AsDataView(); grdReport.DataSource = dv; grdReport.DataBind(); } break; default: break; } </code></pre> <p>And the row data bound event is </p> <pre><code> protected void grdReport_RowDataBound(object sender, GridViewRowEventArgs e) { # region try try { if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; Ob.DatasetMain.Tables[0].Rows.Count != 0) { if ((Ob.FromDate != null || Ob.FromDate != "") &amp;&amp; (Ob.UptoDate != null || Ob.UptoDate != "")) { ((HyperLink)e.Row.Cells[2].FindControl("hplNavigate")).NavigateUrl = String.Format("~//Reports/BookingByCustomerReport.aspx?BC={0},{1},{2},{3}", Ob.DatasetMain.Tables[0].Rows[Ob.Counter][0], Ob.FromDate, Ob.UptoDate, radReportFrom.Checked); Ob.Counter++; } if (hdnFromCustomer.Value == "true") { ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Checked = true; } } if (e.Row.RowType == DataControlRowType.Footer) { if (Ob.DatasetOther.Tables[0].Rows.Count != 0) { e.Row.Cells[2].Text = "Total"; e.Row.Cells[3].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][2].ToString(); e.Row.Cells[4].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][3].ToString(); e.Row.Cells[5].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][4].ToString(); e.Row.Cells[6].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][5].ToString(); } } } # endregion catch (Exception ex) { } } </code></pre> <p>To again list out problem</p> <ol> <li>I cannot sort by des when I click again</li> <li>If I sort out by say, customer name, and then click on <code>qty</code> or any other I get <code>Specified cast is not valid.</code> Means, I cannot sort out by clicking any other column once I sorted by any particular column. </li> </ol> <p>Can anyone help me figure out problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload