How can I show the number of checked checkboxes in a text box from a column in a datagridview?

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

I also have a text box where at the moment the employee enters the number of elements that he moves, then checks the corresponding serial numbers and moves the elements.

I would like the number in the text box to be generated based on the number of serial numbers. Is it possible? At the moment, I have tried about a hundred different solutions, and all of them either end with strange errors, or do not give me any results.

+4
source share
1 answer

Unconfirmed, but it should be close enough. I read an article or ten about it once when I had the same problem. The trick is to immediately fix the edit when you click on the box, which then raises the CellValueChanged event. Then you can pick up an account.

This should update the text box when you check the box and clear the check box:

 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(); } 

Hope Linq works. If not, you will have to do it in the old fashioned way of foreach with a variable amount. I know that DataGridViewRowCollection can sometimes be skinny.

+1
source

Source: https://habr.com/ru/post/1442385/


All Articles