checkbox datagridview全选:
<pre name="code" class="csharp">/// <summary>
/// 全选.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chb_SelectAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgv_Devices.Rows)
{
if (row.Cells[0] is DataGridViewCheckBoxCell)
{
row.Cells[0].Value = chb_SelectAll.Checked;
}
}
}
private void dgv_Devices_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgv_Devices.SelectedRows.Count > 0)
{
DataGridViewRow dataGridViewRow = dgv_Devices.SelectedRows[0];
if (dataGridViewRow.Cells[0] is DataGridViewCheckBoxCell)
{
if (dataGridViewRow.Cells[0].Value == null)
dataGridViewRow.Cells[0].Value = true;
else
{
dataGridViewRow.Cells[0].Value = !((bool)(dataGridViewRow.Cells[0]).Value);
}
}
chb_SelectAll.Checked = dgv_Devices.Rows.Cast<DataGridViewRow>().Where(row => row.Cells[0] is DataGridViewCheckBoxCell).All(row => (bool)row.Cells[0].Value);
}
}