0

I've looked at other posts regarding this but i cant seem to get a value out of a DataGridViewCheckBoxCell besides an empty string.

Here is what I've tried to set the true and false values of the cell upon instancing a new row but to no avail. I'm creating all the columns and rows at runtime btw so the editor doesn't apply.

//Default checkBox true and false values are the same and must be set to true and false
                if (Program.mainForm.TableMainGridView.Columns[column.Name] is DataGridViewCheckBoxColumn)
                {
                    val = false;

                    DataGridViewCheckBoxCell cell = new DataGridViewCheckBoxCell();
                    cell.FalseValue = false;
                    cell.TrueValue = true;

                    Program.mainForm.TableMainGridView.Rows[index].Cells[column.Name] = cell;
                }

I then tried the solution to this question by casting the cell as a DataGridViewCheckBoxCell before retreiving it's value upon the CellContentClick event (with and without me trying to set the cell.FalseValue & cell.TrueValue ) and also to no avail.

DataGridViewCheckBoxCell cell = Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(cell.Value) == true)
                {
                    value = true;
                }
                else
                {
                    value = false;
                }

I also saw that there was a way to set the true and false values of the column itself which would be ideal if it does in fact set the values of all subsequent cells but that doesn't seem to do anything either.

Does anyone else have this issue with creating a DataGridViewCheckBoxColumn and row in script then being able to get a value out of the checkbox cell?

DED
  • 391
  • 2
  • 12

1 Answers1

0

Fixed it, Had to use ".EditedFormatedValue" from an answer in this question Can't Identify Values of DataGridViewCheckBoxCell.

the value seems to remain an empty string that only updates to the correct value and triggers CellValueChanged only on the CellLeave event unless i use .EditedFormatedValue which forces the value to update but in doing so, it triggers CellValueChanged twice for some reason, but it works.

edit: I found that the best way to implement it is to write

Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Convert.ToBoolean(Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue);

in my CellContentClick event to trigger CellValueChanged. Strange, it also seems to have stopped triggering CellContentClick on CellLeave as well. it's a win win, not going to question it.

setting the cell.FalseValue & cell.TrueValue isn't required btw.

DED
  • 391
  • 2
  • 12