简体   繁体   中英

Refresh DataGridView cell values immediately

I have a DataGridView with two checkbox columns. When any rows on both of them are checked, I want to clear the checkmarks. The grid is populated from a 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:

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. No avail; the grid is refreshed, but not redrawn before moving the cursor.

2) CommitEdit() and 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.

1) I click c1 on row r1. c1 becomes checked and r1 becomes selected.

2) I click c2 on row r2 (or even r1). c2 becomes checked and r2 becomes selected.

3) c1 and c2 are now cleared (after logging checkbox info somewhere).

The problem is that, while the chackmark disappears from c1 in r1, it stays there, in the checkbox on r2, until r2 loses focus.

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

brandsGridView.ClearSelection();

I think the easies way is to first set up binding for this DataGridView like:

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.

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

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. So I changed momentarily the focus to another control (a button for example) and then brought it back to the 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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