简体   繁体   English

立即刷新 DataGridView 单元格值

[英]Refresh DataGridView cell values immediately

I have a DataGridView with two checkbox columns.我有一个带有两个复选框列的DataGridView When any rows on both of them are checked, I want to clear the checkmarks.当检查它们两个上的任何行时,我想清除复选标记。 The grid is populated from a DataTable.网格由 DataTable 填充。 It works like this:它是这样工作的:

if (bothColumnsAreChecked)
{
    DataRow first = SelectedFirstItems.First();
    DataRow second = SelectedSecondItems.First();

    // stuff...

    first["IsCheckedFirst"] = false;
    second["IsCheckedSecond"] = false;
}

As per this , I have modified the code to fire the CellValueChanged event immediately after clicking the checkbox, not when losing focus: 据此,我修改了代码以在单击复选框后立即触发CellValueChanged事件,而不是在失去焦点时:

void brandsGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    brandsGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

The problem is that, after clearing the checkmarks, one of the rows is still selected (because I just clicked on a checkbox in that row), and the checkmark on this row is still visible -- it doesn't disappear until I unselect the row.问题是,清除复选标记后,其中一行仍被选中(因为我只是单击了该行中的复选框),并且该行上的复选标记仍然可见——它不会消失,直到我取消选择排。 How do I achieve that?我如何做到这一点? I tried:我试过了:

1) Refresh() on the grid. 1) 在网格上Refresh() No avail;徒劳无功; the grid is refreshed, but not redrawn before moving the cursor.网格被刷新,但在移动光标之前不会重绘。

2) CommitEdit() and EndEdit() . 2) CommitEdit()EndEdit() This doesn't even clear the last selected checkbox.这甚至不会清除最后选择的复选框。

What can I do?我能做什么?

EDIT: I'm bad at explaining.编辑:我不擅长解释。 Here's a simple walkthrough:这是一个简单的演练:

There are two checkbox columns: c1 and c2.有两个复选框列:c1 和 c2。

1) I click c1 on row r1. 1) 我在第 r1 行上单击 c1。 c1 becomes checked and r1 becomes selected. c1 变为选中状态,而 r1 变为选中状态。

2) I click c2 on row r2 (or even r1). 2)我在第 r2 行(甚至 r1)上单击 c2。 c2 becomes checked and r2 becomes selected. c2 变为选中状态,而 r2 变为选中状态。

3) c1 and c2 are now cleared (after logging checkbox info somewhere). 3) c1 和 c2 现在被清除(在某处记录复选框信息后)。

The problem is that, while the chackmark disappears from c1 in r1, it stays there, in the checkbox on r2, until r2 loses focus.问题是,当 r1 中的 c1 中的 c1 标记消失时,它会保留在 r2 上的复选框中,直到 r2 失去焦点。

Hope that helps.希望有帮助。

It's a bit clearer right now.现在有点清楚了。 You could always deselect rows in dgv, but I don't know if that's what you're after你总是可以取消选择 dgv 中的行,但我不知道这是否是你想要的

brandsGridView.ClearSelection();

I think the easies way is to first set up binding for this DataGridView like:我认为最简单的方法是首先为此 DataGridView 设置绑定,例如:

DataGridView dg = new DataGridView();
DataTable dt = GetDataTable();
dg.DataSource = dt;

And then when you want to refresh it you set up binding again.然后当你想刷新它时,你再次设置绑定。 Yes, I realize it is dirty hacky way but it is the easiest.是的,我意识到这是肮脏的hacky方式,但它是最简单的。

DataTable dt = GetDataTable();
dg.DataSource = dt;

Old answer: I think you could use BindingGroup for that: http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx旧答案:我认为您可以为此使用 BindingGroup: http : //msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx

I had a very similar problem.我有一个非常相似的问题。 (Well it is a different context, but the problem was the same) (嗯,这是一个不同的上下文,但问题是一样的)

After a lot of trying and reading, this and other related threads, I found a solution.经过大量的尝试和阅读,这个和其他相关的线程,我找到了一个解决方案。 It is a bit rudimentary but it works for me.这有点简陋,但对我有用。

The problem was related with the DataGridView having the focus.该问题与具有焦点的DataGridView相关。 So I changed momentarily the focus to another control (a button for example) and then brought it back to the DataGridView所以我暂时将焦点更改为另一个控件(例如按钮),然后将其带回DataGridView

In your case could be like this:在你的情况下可能是这样的:

button1.focus();
if (bothColumnsAreChecked)
{
    DataRow first = SelectedFirstItems.First();
    DataRow second = SelectedSecondItems.First();

    // stuff...

    first["IsCheckedFirst"] = false;
    second["IsCheckedSecond"] = false;
}
brandsGridView.focus();

I hope it could help.我希望它能有所帮助。 But if someone finds a better solution or a more detailed explanation about why this is happening.但是,如果有人找到了更好的解决方案或更详细的解释为什么会发生这种情况。 It would be highly appreciated.将不胜感激。

在设置所需的单元格值后使用以下命令对我有用,这是我在Microsoft 论坛帖子上找到的。

brandsGridView.RefreshEdit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM