Hi bunty_20k,
To achieve your goal, you can handle the CellValueChanged to change the value of the CheckBox cell. This is the code snippet:
private void Form1_Load(object sender, EventArgs e)
{
//Handle CellValueChanged event to set the value of the CheckBoxCell.
this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == this.checkColumn.Index)
{
this.dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
//Change the value of the checkBox cell according to whether comboBox cell has a value.
if (this.dataGridView1.CurrentRow.Cells[this.comboColumn.Index].Value != null)
this.dataGridView1.CurrentCell.Value = true;
else
this.dataGridView1.CurrentCell.Value = false;
this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
}
But based on my understanding, you want the CheckBox column to show whether the ComboBox cell in the same row has a value or not. In this case, we need to have the CheckBox column read only and handle the CellValueChanged event to check/uncheck the CheckBox cell according to whether the ComboBox cell has a value or not. This is the code snippet:
private void Form1_Load(object sender, EventArgs e)
{
//Set check column read only to avoid editing.
this.checkColumn.ReadOnly = true;
//Handle CellValueChanged event to set the value of the CheckBoxCell.
this.dataGridView1.CellValueChanged +=new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == this.comboColumn.Index)
{
this.dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
//If comboBox has value, check the checkBox, elase uncheck it.
if (this.dataGridView1.CurrentCell.Value != null)
this.dataGridView1.CurrentRow.Cells[this.checkColumn.Index].Value = true;
else
this.dataGridView1.CurrentRow.Cells[this.checkColumn.Index].Value = false;
this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
}
Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.