修改数据表 C#

发布于 2024-11-06 13:09:11 字数 805 浏览 0 评论 0原文

我的数据表中有一个数据列表,我还有一个按钮,单击该按钮将显示上周已完成的行。

但是,由于某种原因,我无法在修改后更新表

,因为我得到了 7 天前的日期,并且对于完成日期大于源 DataTable 中的每一行,我删除了该行。这应该只留下上周完成的行,但由于某种原因,在我的方法完成后,每一行仍然保留。有人能发现这里有问题吗?

预先感谢您的任何帮助!

protected void btnShowLastWeek_OnClick(Object sender, EventArgs e)
    {
        DateTime current = DateTime.Today;
        DateTime lastWeek = current.AddDays(-7);

        DataTable temp = compDV.Table;

        for(int i = 0; i < temp.Rows.Count; i ++)
        {
            DateTime completed = (DateTime)temp.Rows[i]["DateCompleted"];

            if (completed.CompareTo(lastWeek.Date) <= 0)
            {
                temp.Rows.RemoveAt(i);
            }
        }

        dgCompletedRequests.DataSource = temp;
        dgCompletedRequests.DataBind();
    }

I have a list of data in a data table, I also have a button that when clicked Will Show the Rows that have been completed in the last week.

However, I cant get the table to update after the modifications for some reason

I get the date of 7 days ago, and for every row that has a completed date greater than it in the source DataTable, I delete that row. That should leave me with only rows completed in the last week, but for some reason every row remains after my method is done. Can anyone spot a problem here?

Thanks in advance for any help!

protected void btnShowLastWeek_OnClick(Object sender, EventArgs e)
    {
        DateTime current = DateTime.Today;
        DateTime lastWeek = current.AddDays(-7);

        DataTable temp = compDV.Table;

        for(int i = 0; i < temp.Rows.Count; i ++)
        {
            DateTime completed = (DateTime)temp.Rows[i]["DateCompleted"];

            if (completed.CompareTo(lastWeek.Date) <= 0)
            {
                temp.Rows.RemoveAt(i);
            }
        }

        dgCompletedRequests.DataSource = temp;
        dgCompletedRequests.DataBind();
    }

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

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

发布评论

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

评论(3

心欲静而疯不止 2024-11-13 13:09:11

您应该绑定到 DataView:

dgCompletedRequests.DataSource =
    new DataView(compDV.Table, "DateCompleted > #" + lastWeek + "#");

You should bind to a DataView instead:

dgCompletedRequests.DataSource =
    new DataView(compDV.Table, "DateCompleted > #" + lastWeek + "#");
悲念泪 2024-11-13 13:09:11

我会遵循 SLAks 的建议,但归根结底,我也会考虑下注,只需重写代码来查询数据表,而不修改原始代码。

dgCompletedRequests.DataSource = 
      temp.AsEnumerable()
          .Where(row => row.Field<Datetime>("DateCompleted") >= lastWeek)
          .AsDataView(); // or .CopyToDataTable(); if you need it

I would follow SLaks' advice from the comments, but when it comes down to it, I would also consider punting and just rewrite the code to query the datatable without modifying the original.

dgCompletedRequests.DataSource = 
      temp.AsEnumerable()
          .Where(row => row.Field<Datetime>("DateCompleted") >= lastWeek)
          .AsDataView(); // or .CopyToDataTable(); if you need it
刘备忘录 2024-11-13 13:09:11

在此代码执行之前,dgCompletedRequests.DataSource 是否已设置?我不确定这是否适合您,但过去对我有用,您可以尝试一下。设置 dgCompletedRequests.DataSource = null;然后将其设置为温度。不知何故,我认为这将“重置”数据绑定。

Was dgCompletedRequests.DataSource already set before this code executes? I'm not sure if this will work for you but it has for me in the past, you might give this a try. Set your dgCompletedRequests.DataSource = null; then set it to temp after. Somehow I think this will "reset" the databinding.

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