我搜索了很多次,但没有任何有用的链接来解决我的问题。我有AllwSorting =“true”和OnSort函数。今天我发布这个问题,谢谢你的帮助。 这是我的ASCX:
<asp:GridView ID="gvwCus" runat="server" CssClass="gridview" AlternatingRowStyle-CssClass="even" AutoGenerateColumns="false" GridLines="Vertical" AllowPaging="false" OnRowCommand="RowCommand" AllowSorting="true" OnSorting="gvwCus_OnSorting">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="4%">
<HeaderTemplate>STT</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text="<%#Container.DataItemIndex + 1%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderStyle-Width="12%" DataField="field1" HeaderText="Field1_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderStyle-Width="30%" DataField="field2" HeaderText="Field2_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderStyle-Width="31%" DataField="field3" HeaderText="Field3_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderStyle-Width="20%" DataField="field4" HeaderText="Field4_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderStyle-Width="2%" ButtonType="Image" SelectImageUrl="~/Images/detail.png" ShowSelectButton="true" />
</Columns>
</asp:GridView>
我的代码背后:ASCX.CS,我有排序事件的gvwCus_OnSorting函数,但是当我点击gridview的标题时没有处理任何事件。
protected void gvwCus_OnSorting(object sender, GridViewSortEventArgs e)
{
DataBind(e);
}
private void DataBind(GridViewSortEventArgs eventArgs)
{
var dataOracle = new DataOracle();
var data = dataOracle.GetDataTable();
if(eventArgs != null)
{
data.DefaultView.Sort = eventArgs.SortExpression + " " + GetSortDirection(eventArgs.SortExpression);
}
gvwCustom.DataSource = data;
gvwCustom.DataBind();
}
private string GetSortDirection(string column)
{
string sortDirection = "DESC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "DESC"))
{
sortDirection = "ASC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
谢谢!
答案 0 :(得分:2)
您没有为要排序的列设置 SortExpression 。添加SortExpression,您将看到每列的超链接。单击这些链接将触发排序事件。添加sortexpression后,您的boundfields应如下所示:
<asp:BoundField SortExpression="field1" HeaderStyle-Width="12%" DataField="field1" HeaderText="Field1_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField SortExpression="field2" HeaderStyle-Width="30%" DataField="field2" HeaderText="Field2_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField SortExpression="field3" HeaderStyle-Width="31%" DataField="field3" HeaderText="Field3_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField SortExpression="field4" HeaderStyle-Width="20%" DataField="field4" HeaderText="Field4_Header" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center" />
呈现的标题应如下所示: