如何让 DataTable 的内容在作用域结束后仍然存在?

发布于 2024-11-30 03:38:09 字数 720 浏览 0 评论 0原文

我有一个 DataTable,它是在课程开始时声明的,如下所示:

private DataTable table = new TestData().FillTable();

在整个课程中,我可以访问表的内容。

但是,我有一个 GridView 排序事件,它会更改 DataTable 的内容。在事件本身中,内容发生了变化,我已经通过调试确认了这一点。但是,如果我从该方法之外的任何地方调用该表,DataTable 的内容将保持原来的状态,即一旦排序事件的范围结束,新值似乎就会被删除。

这是我的排序事件的代码:

protected void TableGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        table.DefaultView.Sort = "GroupNumber, " + e.SortExpression + GetSortDirectio(e.SortExpression);
        TableGridView.DataSource = table;
        TableGridView.DataBind();
    }

当我在上述事件中测试表的内容时,似乎添加了新值。但是,如果我从任何其他方法调用该表,则会返回旧值。

我该怎么做才能使表的内容全局更新,即更新到类开头声明的变量,并且根据方法不具有不同的状态?

I've got a DataTable which is declared at the beginning of my class like so:

private DataTable table = new TestData().FillTable();

Throughout the class, I'm able to access the table's contents.

However, I've got a GridView sorting event which changes the contents of the DataTable. Within the event itself, the contents is changed and I've confirmed this through debugging. However, if I call the table from anywhere outside of this method, the contents of the DataTable remains as it was before, i.e. the new values seem to be erased once the soerting event's scope is over.

Here is the code of my sorting event:

protected void TableGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        table.DefaultView.Sort = "GroupNumber, " + e.SortExpression + GetSortDirectio(e.SortExpression);
        TableGridView.DataSource = table;
        TableGridView.DataBind();
    }

When I test the table's contents within the above event, the new values seem to be added. However, if I call the table from any other method, the old values are returned.

What can I do to make the table's contents be updated globally, i.e. to the variable declared at the beginning of my class, and not have different states depending on methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-12-07 03:38:09

我已经用 GridView 排序事件对此进行了测试。离开排序事件 this.table 包含排序值,因此它按预期工作。您确定正在单页请求中测试您的解决方案吗?回发后,this.table 再次从 TestData().FillTable() 分配。

I've tested this with GridView Sorting event. Leaving sorting event this.table contains sorted values so it works as exptected. Are you sure you are testing your solution in single page request. After PostBack this.table is assigned from TestData().FillTable() again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文