4

I have a DataGridView with a CheckBox column in it. (This is an inventory system.) The check is used to move serial numbers of items from one location to another in our company.

I also have a textbox where, as of now, the employee enters the number of items he is moving, then checks the appropriate serial numbers, and moves the items.

I would like the number in the textbox to be generated depending on how many serial numbers are checked. Is it even possible? I've tried about a hundred different solutions at this point and they all either end in strange errors, or give me no results whatsoever.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • So to be clear, you just need to update a textbox with the number of checkboxes checked in the datagridview? Does this need to update as you check/uncheck items? – ImGreg Oct 26 '12 at 18:54

1 Answers1

1

Untested, but this should be close enough. I read an article or ten about this once when I was having the same issue. The trick is to commit the edit immediately when the box is clicked on, which will then trigger the CellValueChanged event. You can then pick up the count from there.

This should update the textbox as you check and uncheck the checkbox:

private void dgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dgv.IsCurrentCellDirty && dgv.CurrentCell.OwningColumn.Name == "MyCheckColumn")
        dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1) //just a guard for the header row
        return;

    if (dgv.Columns[e.ColumnIndex].Name == "MyCheckColumn")
        textBox.Text = dgv.Rows.Cast<DataGridViewRow>().Count(r => Convert.ToBoolean(r.Cells["MyCheckColumn"].Value)).ToString();
}

Hopefully the Linq works. If not, you'll have to do it the old-fashioned foreach way with a sum variable. I know the DataGridViewRowCollection can be finicky sometimes.

lc.
  • 113,939
  • 20
  • 158
  • 187
  • Where is the 'WriteoffData.CommitEdit...' coming from? I don't seem to have anything with that.. Do I need to add a library? –  Oct 26 '12 at 19:01
  • @HerNameIsEllie It's a bad paste, sorry. See my edited version. – lc. Oct 26 '12 at 19:02
  • Sigh, I'm now getting some weird error that doesn't seem to be related. "System.FormatException: Formatted value of the cell has a wrong type." I think I messed something up while I was trying to do this earlier (I have lots of edits). Any idea where that's coming from? –  Oct 26 '12 at 19:09
  • @HerNameIsEllie Ouch, are you trying to programmatically parse the value of a cell, specifically using the `CellFormatting` event? If so, make sure you're setting `e.Value` to the type specified in `e.DesiredType` (I think it's usually `string` but could be wrong) – lc. Oct 26 '12 at 19:12
  • I don't think I have a CellFormatting event... Should I? –  Oct 26 '12 at 19:14
  • @HerNameIsEllie No, it's just one common source of that kind of error. I think it may just be time to start commenting out code until you find the source... – lc. Oct 26 '12 at 19:16
  • Or, if you're databound, you will have another problem entirely which may cause this - that the default `GetFormattedValue` for the `DataGridViewCheckBoxColumn` really only wants a a)Boolean column bound to it or b)a string, where TrueValue and FalseValue are set properly. This could be where the error is coming from too and equally ugly to fix – lc. Oct 26 '12 at 19:22