修改数据表 C#
我的数据表中有一个数据列表,我还有一个按钮,单击该按钮将显示上周已完成的行。
但是,由于某种原因,我无法在修改后更新表
,因为我得到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该绑定到 DataView:
You should bind to a
DataView
instead:我会遵循 SLAks 的建议,但归根结底,我也会考虑下注,只需重写代码来查询数据表,而不修改原始代码。
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 是否已设置?我不确定这是否适合您,但过去对我有用,您可以尝试一下。设置 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.