问题
How can I programmatically uncheck all rows in a DataGridViewCheckboxColumn in a datagridview?
I can get the correct value of the checkbox using
(bool)row.Cells[CheckBoxColumn.Index].FormattedValue
but that's only a getter.
I have tried setting the value of the cell using
(bool)row.Cells[CheckBoxColumn.Index].value = false
but that doesn't affect the FormattedValue.
How can I solve this?
回答1:
You do sth. like:
(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
You just forgot to cast to the correct type, a generic DataGridViewCell doesn't know its value-type.
回答2:
Haven't checked but you can try;
CheckBox cb = (row.Cells[CheckBoxColumn.Index].Controls[0] as CheckBox);
if(cb != null)
{
cb.Checked = false;
}
It's type may be different. Just debug and cast it to what it is.
回答3:
Have you tried casting the first control in the checkbox column to checkbox and then setting 'Checked' to true?
Try something to this extent.
((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true
回答4:
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
dr.Cells[0].Value = true;//sıfırın
}
回答5:
If you use dataGridView1_ContextClick just for do "false" datagidviewCheckBox Column need this Code :
dataGridView1.CancelEdit();
but if you need all rows of CheckBoxColumns of DataGrid :
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
r.Cells["statusBox"].Value = true;
}
}
回答6:
Loop through each row of grid view and use the find control method:
foreach ( GridViewRow row in myGridView )
{
CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
checkbox.Checked = false;
}
回答7:
foreach (DataGridViewRow row in datagridviewname.Rows)
{
row.Cells[CheckBoxColumn1_Name].Value = false;
}
来源:https://stackoverflow.com/questions/4725384/programmatically-uncheck-checkboxcolumn-in-datagridview