在C#中,DataGridView控件默認不支持多選操作
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = true;
dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
dataGridView1.MultiSelect = true;
}
else
{
dataGridView1.MultiSelect = false;
}
}
dataGridView1.CellMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseClick);
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (Control.ModifierKeys == Keys.Control)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
dataGridView1.Rows[e.RowIndex].Selected = !dataGridView1.Rows[e.RowIndex].Selected;
}
}
}
現(xiàn)在,DataGridView控件應(yīng)該可以處理多選事件了。用戶可以按住Ctrl鍵并單擊行頭部或單元格來選擇多行。