私はこのグリッドビューを持っています、それは2つの問題を抱えています。
- 列をクリックして並べ替えてからもう一度クリックすると、順序どおりに並べ替えられません。
一度aで並べ替えて
col A
から、他の列をクリックすると、再度並べ替えられませんか?<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"/>
ソートの背後にあるコードは
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;
}
そして、行データバインドイベントは
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)
{ }
}
問題を再度リストアップするには
- もう一度クリックすると、desで並べ替えることができません
- たとえば、顧客名で並べ替えてから、
qty
またはその他の[Specified cast is not valid.
手段]をクリックすると、特定の列で並べ替えた後、他の列をクリックして並べ替えることができなくなります。
誰かが私が問題を理解するのを手伝ってくれる?