1

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.

Community
  • 1
  • 1
John NoCookies
  • 1,041
  • 3
  • 17
  • 28
  • I don't understand. Selecting rows in a datagridview and having checkmarks in some column (which I assume you have from what you've written) are 2 different things. What are you trying to achieve? The selection to vanish or what exactly? – Tarec Aug 22 '13 at 16:00
  • When I click a checkbox, the row with the checkbox is selected. The selection might vanish or not, it's not important. What I want to do is redraw the grid after clearing the checkboxes, so that the checkmarks are not there. – John NoCookies Aug 22 '13 at 16:06
  • @JohnNoCookies I don't understand what you mean by `clear a checkbox`? You draw it yourself and you want to clear it? Or you mean `uncheck a checkbox`? – King King Aug 22 '13 at 16:16
  • Yes, I mean uncheck. I want to remove the visual checkmark. Because that's what's happening in the underlying DataTable (the bool behind the checkbos is set to false), but the checkmark lingers. – John NoCookies Aug 22 '13 at 16:32
  • @JohnNoCookies where do you place this if `if (bothColumnsAreChecked)`? that will deselect all the checkboxes from underlying data (I guess it's a DataTable?). – King King Aug 22 '13 at 16:54

4 Answers4

0

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();
Tarec
  • 3,268
  • 4
  • 30
  • 47
  • This does unselect the row, but the checkmark is still there. – John NoCookies Aug 22 '13 at 16:32
  • Yes, but I suppose the reason one checkmark deselects while other not is because second row is still selected, so you could just try clearing selection before deselecting checkboxes in the method you provided. – Tarec Aug 22 '13 at 16:51
0

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

Pawel Troka
  • 853
  • 2
  • 12
  • 33
0

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.

Oscar Hermosilla
  • 480
  • 5
  • 21
0

Using the following command after setting the desired cell values worked for me, which I found on a Microsoft forum thread.

brandsGridView.RefreshEdit();

gotorg
  • 147
  • 1
  • 10