这可能是您正在寻找的:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
listBindByName(); //this would be your procedure to look for the data you want
DataSet dsSortTable = GridView1.DataSource as DataSet;
DataTable dtSortTable = dsSortTable.Tables[0];
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
ViewState["sortExpression"] = e.SortExpression;
GridView1.DataSource = dvSortedView;
GridView1.DataBind();
}
UpdatePanel1.Update();
}
private string getSortDirectionString()
{
if (ViewState["sortDirection"] == null)
{
ViewState["sortDirection"] = "ASC";
}
else
{
if (ViewState["sortDirection"].ToString() == "ASC")
{
ViewState["sortDirection"] = "DESC";
return ViewState["sortDirection"].ToString();
}
if (ViewState["sortDirection"].ToString() == "DESC")
{
ViewState["sortDirection"] = "ASC";
return ViewState["sortDirection"].ToString();
}
}
return ViewState["sortDirection"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
这是TemplateField的一个示例:
<asp:TemplateField HeaderText="Description" SortExpression="description">
<ItemTemplate>
<asp:Label Visible="true" runat="server" ID="descriptionLabel" Text='<%# bind("description") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditDescription" Width="100px" runat="server" Text='<%#Bind("description") %>' />
</EditItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
通过添加SortExpression属性,GridView标头将变为可单击.确保sort表达式属性是您通过sql查询绑定的字段的名称.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
14145 次 |
| 最近记录: |