gpt4 book ai didi

c# - 如何在 asp.net 中对 GridView 中的对象列表进行排序?

转载 作者:行者123 更新时间:2023-11-30 21:47:55 25 4
gpt4 key购买 nike

我有一个对象列表

List<News> theNews = controlClass.ListNews();

ListNews() 加载了数据集中的新闻对象。

然后我有一个gridview

    <asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="False"  OnSelectedIndexChanged="gvNews_SelectedIndexChanged" AllowSorting="True" OnSorting="gvNews_Sorted">
<Columns>
<asp:BoundField DataField="titNew" HeaderText="Title" />
<asp:BoundField DataField="dateNew" HeaderText="Date" SortExpression="dateNew" />
<asp:BoundField DataField="typeNew" HeaderText="Type" />
<asp:CommandField SelectText="See More" ShowSelectButton="True" />
</Columns>
</asp:GridView>

我将所述列表存储到此 GridView 中,然后我使用以下代码对其进行排序

    private void LoadNews()
{
this.gvNews.DataSource = controlClass.ListNews();
this.gvNews.DataBind();
ViewState["dt"] = controlClass.ListNews();
ViewState["sort"] = "Asc";
}

protected void gvNews_Sorted(object sender, GridViewSortEventArgs e)
{
DataTable result = (DataTable)ViewState["dt"];
if (result.Rows.Count>0)
{
if(ViewState["sort"].ToString()=="Asc")
{
result.DefaultView.Sort = e.SortExpression + " Desc";
ViewState["sort"] = "Desc";
}
else
{
result.DefaultView.Sort = e.SortExpression + " Asc";
ViewState["sort"] = "Asc";
}
gvNews.DataSource = result;
gvNews.DataBind();
}
}

但这导致我出现以下错误: 在程序集“MySolution,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”中键入“MySolution.News”未标记为可序列化。我似乎无法将我的列表存储在 ViewState 中,有解决办法吗?

更新:我已经使我的新闻类可序列化,但是当我单击 GridView 中的日期标题时出现以下错误

无法将“System.Collections.Generic.List`1[MySolution.News]”类型的对象转换为类型“System.Data.DataTable”。

更新编号 2:我修改了我的 gvNews_Sorted 方法以匹配@Dr.Stitch 的答案。

protected void gvNews_Sorted(object sender, GridViewSortEventArgs e)
{
List<News>result = (List<News>)ViewState["dt"];
if (result.Count>0)
{
if(ViewState["sort"].ToString()=="Asc")
{
result = result.OrderBy(e.SortExpression + " Desc").ToList();
ViewState["sort"] = "Desc";
}
else
{
result = result.OrderBy(e.SortExpression + " Asc").ToList();
ViewState["sort"] = "Asc";
}

gvNews.DataSource = result;
gvNews.DataBind();
}
}

我在行中收到以下错误

result = result.OrderBy(e.SortExpression + " Desc").ToList();

enter image description here

更新 3: 我在尝试 Dr Stitch 的新答案时做了进一步的更改,现在尽管不再崩溃,但它什么也没做,当我点击标题时没有任何变化

最佳答案

您正在保存 List<News>对象

ViewState["dt"] = controlClass.ListNews();

但试图检索 DataTable对象

DataTable result = (DataTable)ViewState["dt"];

你必须改变你的gvNews_Sorted对此:

protected void gvNews_Sorted(object sender, GridViewSortEventArgs e)
{
List<News>result = (List<News>)ViewState["dt"];
if (result.Count>0)
{
if(ViewState["sort"].ToString()=="Asc")
{
if ("titNew" == e.SortExpression)
result = result.OrderByDescending(r => r.titNew).ToList();
//...do it to all the fields

ViewState["sort"] = "Desc";
}
else
{
if ("titNew" == e.SortExpression)
result = result.OrderBy(r => r.titNew).ToList();
//...do it to all the fields
ViewState["sort"] = "Asc";
}

gvNews.DataSource = result;
gvNews.DataBind();
ViewState["dt"] = result;
}
}

确保 LoadNews()仅调用非回发事件

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadNews();
}
}

关于c# - 如何在 asp.net 中对 GridView 中的对象列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38047753/

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