I have this grid view, it has 2 problems.
- if I sort it by clicking on a column, and then I click again, its not sorted in des order.
-
If once I sort with a
col Aand then click on any other column its not sorted again?<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"> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField Visible="false"> <ItemTemplate> <asp:Label ID="lblCustCodes" runat="server" Text='<%# Eval("CustCode") %>' CssClass="grdCustName"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Customer" SortExpression="Customer"> <ItemTemplate> <asp:HyperLink Target="_blank" Text='<%# Eval("CustomerName") %>' runat="server" ID="hplNavigate"> </asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="QTY" HeaderText="Booked Qty" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" SortExpression="QTY"> <FooterStyle HorizontalAlign="Right" /> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Volume" HeaderText="Booked Amt" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" SortExpression="Volume"> <FooterStyle HorizontalAlign="Right" /> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> </asp:BoundField> <asp:BoundField DataField="FirstBill" HeaderText="First Bill" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left" SortExpression="FirstBill"> <FooterStyle HorizontalAlign="Left" /> <ItemStyle HorizontalAlign="Left"></ItemStyle> </asp:BoundField> </Columns> <FooterStyle BackColor="Aqua" Font-Bold="true" ForeColor="BlueViolet"/>
The code behind for sorting is
switch (e.SortExpression)
{
case "Customer":
if (e.SortDirection == SortDirection.Ascending)
{
var result = from table in Ob.DataTableOther.AsEnumerable()
orderby table.Field<string>("CustomerName")
select table;
var dv = result.AsDataView();
grdReport.DataSource = dv;
grdReport.DataBind();
}
else
{
var result = from table in Ob.DataTableOther.AsEnumerable()
orderby table.Field<string>("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<int>("Qty")
select table;
var dv = result.AsDataView();
grdReport.DataSource = dv;
grdReport.DataBind();
}
else
{
var result = from table in Ob.DataTableOther.AsEnumerable()
orderby table.Field<int>("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<float>("Volume")
select table;
var dv = result.AsDataView();
grdReport.DataSource = dv;
grdReport.DataBind();
}
else
{
var result = from table in Ob.DataTableOther.AsEnumerable()
orderby table.Field<float>("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<DateTime>("FirstBill")
select table;
var dv = result.AsDataView();
grdReport.DataSource = dv;
grdReport.DataBind();
}
else
{
var result = from table in Ob.DataTableOther.AsEnumerable()
orderby table.Field<DateTime>("FirstBill") descending
select table;
var dv = result.AsDataView();
grdReport.DataSource = dv;
grdReport.DataBind();
}
break;
default:
break;
}
And the row data bound event is
protected void grdReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
# region try
try
{
if (e.Row.RowType == DataControlRowType.DataRow && Ob.DatasetMain.Tables[0].Rows.Count != 0)
{
if ((Ob.FromDate != null || Ob.FromDate != "") && (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)
{ }
}
To again list out problem
- I cannot sort by des when I click again
- If I sort out by say, customer name, and then click on
qtyor any other I getSpecified cast is not valid.Means, I cannot sort out by clicking any other column once I sorted by any particular column.
Can anyone help me figure out problem?
you have to allow your gridview to sort on your gridview property
moreover you have to store your sort in cache (viewstate or session).use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.
Example: