enter image description hereI want to make n 'all select' button in datagridview. datagirdview has checkbox column. If I press the Select All button, I can select the entire selection. If I press the Select All button again, I want to release the entire selection. I can only Select All, no DeSelect All . Please help me :(
private void Btn_selectall_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
item.Selected = true;
item.Cells[0].Value = true;
}
}
You could try something like this:
private void Btn_selectall_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Cast<DataGridViewRow>().All(r => r.Selected))
{
// deselect all
foreach (DataGridViewRow item in dataGridView1.Rows)
{
item.Selected = false;
item.Cells[0].Value = false;
}
}
else
{
// select all
foreach (DataGridViewRow item in dataGridView1.Rows)
{
item.Selected = true;
item.Cells[0].Value = true;
}
}
}