在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;
}
}
}
现在,DataGridView控件应该可以处理多选事件了。用户可以按住Ctrl键并单击行头部或单元格来选择多行。