I am confident there are numerous ways to do this. Below is one possible way to manage the progress bar such that the “checked” check boxes in a grid’s column dictate the progress bars current value.
If no check boxes are checked, then the progress bar would be at zero. If all the check boxes are checked, then the progress bar would be full. If half the check boxes are checked, then the progress bar would show half filled.
It is unclear if the user is allowed to “add” rows. I will assume that the rows are fixed when the grid is loaded and the user is not able to “add” rows to the grid. Otherwise, the code would have to “update" the maximum value for the progress bar when the row is added.
A small trace of the current code...
The current posted code is a little odd and is doing a lot of unnecessary work. For starters, the for loop looks odd in a sense that this code is executed in the grids CurrentCellDirtyStateChanged event… So, this event fires every time a single cells value becomes dirty or is changed. So technically, only ONE (1) cell has changed. It seems odd to loop through all the check box values when only a single check box value may have changed.
I assume this is because the code is not keeping track of the progress bars current value. So, this value needs to be computed every time a single check box value changes. I am confident, there is an easier approach to change the progress bars value even without keeping track of its current value. I am thinking to add 1 to the progress bar when a check box is checked and subtract 1 when it is unchecked.
In addition, it is odd the way the current code is “checking” the value. There is a line before the for loop…
string A = this.norms_TableDataGridView.Rows[i].Cells[2].Value.ToString();
Then, in the for loop there is a “check” to see if A is “True”?
if (A == "True")
This is odd because A never changes in the loop. If it is true, then it will ALWAYS be true in the loop. Same for false. Therefore, s_Checks++ will always either be zero or equal to cnt when the for loop exits.
To fix this, you would need to “change” A to the next check box value with each iteration of the loop. However, even with this fix, the code is still checking values that have not changed since it was last checked.
Again, there may be an easier approach.
A different approach....
I am assuming that when the grid is loaded, that all the check boxes are NOT checked and the progress bar would start at zero. In addition, when the grid is loaded with data, the progress bars Maximum value will be set to the number of (fixed) rows in the grid.
With this approach, the code will “add” 1 to the progress bars value when a check box is changed from “unchecked” to “checked.” And, the code will “subtract” 1 from the progress bars value if a “checked” box is “unchecked.”
To avoid errors, and to keep it simple, the code simply checks to make sure that adding 1 to the progress bars value does not go over its Maximum value. In addition, a check to make sure that subtracting 1 does not go below the Minimum value.
So, this approach would go something like below. As soon as the grids data is loaded where all the check boxes are NOT checked, we would set the progress bars, min and max values. Something like…
private void Form2_Load(object sender, EventArgs e) {
dataGridView1.DataSource = YourDataSource;
progressBar1.Maximum = dataGridView1.Rows.Count;
progressBar1.Minimum = 0;
}
Instead of using the grids CurrentCellDirtyStateChanged event, I used the grids CellContentClick event.
One issue with using the CellContentClick event is that when it fires, the cells value has not changed yet. We could easily reverse the checking logic, however, to keep the code in a readable state, I will call the grids CommitEdit to update the changes in the grid.
In the event, the code checks if the check box is checked… in this example, the check box is in column 1.
(bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value == true
If the check box went from “unchecked” to “checked,” then the code will add 1 to the progress bars value and subtract 1 from its value if the check box went from “checked” to “unchecked.”
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 1) {
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value == true) {
if (progressBar1.Value < progressBar1.Maximum) {
progressBar1.Value++;
}
}
else {
if (progressBar1.Value > 0) {
progressBar1.Value--;
}
}
}
}
I hope this makes sense.