分拣asp.net/c#的GridView [英] sorting gridview in asp.net/c#
本文介绍了分拣asp.net/c#的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有定义的GridView如下:
I have a gridview which is defined as follows:
<asp:GridView ID="ComponentGridView" runat="server" AutoGenerateColumns="true" OnPageIndexChanging="ComponentGridView_PageIndexChanging" OnSorting="ComponentGridView_Sorting">
</asp:GridView>
和这些是被调用的ComponentGridView_Sorting方法
and these are the methods that get invoked on ComponentGridView_Sorting
protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = ComponentGridView.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
ComponentGridView.DataSource = dataView;
ComponentGridView.DataBind();
}
}
和
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
但好像没事的时候我就列标题,单击正在发生的事情。
But it seems like nothing is happening when i click on the column headers.
我究竟做错了什么?
请帮我
谢谢期待
当我调试的的dataTable 为空
DataTable中的dataTable = ComponentGridView.DataSource如数据表;
DataTable dataTable = ComponentGridView.DataSource as DataTable;
推荐答案
记住,默认情况下,这是无状态的。所以,你需要一个参考保留您的数据源,还是为数据再次查询一次。
Keep in mind that by default, this is stateless. So you'll need to retain a reference to your datasource, or query once again for the data.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Rows.Add(new object[] { 5, 1 });
dt.Rows.Add(new object[] { 4, 2 });
dt.Rows.Add(new object[] { 3, 3 });
dt.Rows.Add(new object[] { 2, 4 });
dt.Rows.Add(new object[] { 1, 5 });
this.ComponentGridView.DataSource = dt;
this.ComponentGridView.DataBind();
Session["data"] = dt;
}
}
protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = Session["data"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
ComponentGridView.DataSource = dataView;
ComponentGridView.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
这篇关于分拣asp.net/c#的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文