gpt4 book ai didi

c# - 使 DataGridViewColumn 可排序

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:25 25 4
gpt4 key购买 nike

我在 ASP NET 4 上使用 C-sharp。

我需要向 GridView 中的列添加排序功能。我已将 GridView 上的 AllowSorting 属性设置为 true 并将排序表达式添加到该列。

不幸的是,排序在 GridView 中不起作用。

下面是我应该能够排序的列的代码隐藏文件,但我得到了错误

CS1502: The best overloaded method match for 'System.Data.DataView.DataView(System.Data.DataTable)' has some invalid arguments

在这一行:

DataView sortedView = new DataView(BindData());

代码隐藏:

string sortingDirection;    

public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}

public string SortField
{
get
{
return (string)ViewState["SortField"] ?? "Name";
}
set
{
ViewState["SortField"] = value;
}
}

protected void gvProducts_Sorting(object sender, GridViewSortEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}

DataView sortedView = new DataView(RetrieveProducts());

sortedView.Sort = e.SortExpression + " " + sortingDirection;
SortField = e.SortExpression;
gvProducts.DataSource = sortedView;
gvProducts.DataBind();
}

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (dir == SortDirection.Ascending)
{
sortingDirection = "Asc";
}
else
{
sortingDirection = "Desc";
}

DataView sortedView = new DataView(RetrieveProducts());
sortedView.Sort = SortField + " " + sortingDirection;
gvProducts.DataSource = sortedView;
gvProducts.PageIndex = e.NewPageIndex;
gvProducts.DataBind();
}

private void BindData()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}

private DataSet RetrieveProducts()
{

DataSet dsProducts = new DataSet();

string sql = " ... ";

using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
cn.Open();

using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
........
}
}

return dsProducts;
}

编辑 #1

DataView sortedView = new DataView(dsProducts.Tables[0]);

编辑#2

我在aspx页面中添加了:

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

但是如果在 Column Name 中点击我有这个新错误:

System.IndexOutOfRangeException: Cannot find table 0.

在这一行:

Line 100:        DataView sortedView = new DataView(dsProducts.Tables[0]);

最佳答案

DataView类只有三个构造函数,其中一个是默认构造函数DataView(),第二个以DataTable为参数DataView(DataTable),另一个以四个参数DataView(DataTable, String, String, DataViewRowState) .

DataView 构造函数需要这些类型中的任何一种参数,但您的代码具有其他类型的参数。那是错误。

您的 BindData 方法应该返回一个 DataTable 对象,

//This function should return a Datatable
private void BindData()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}

您可以在此处将其传递到您的 DataView。

DataView sortedView = new DataView(BindData());

对于您的第二次编辑,

System.IndexOutOfRangeException: Cannot find table 0.

在这一行:

Line 100:        DataView sortedView = new DataView(dsProducts.Tables[0]);

我猜数据集是空的,错误明确指出数据集中位置0处没有任何表。因此请检查您的数据集是否有表。可能是你的 sql 请求没有得到任何表来填充数据集。否则您可能已经创建了数据集的新实例。

关于c# - 使 DataGridViewColumn 可排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com