谁能告诉我如何防止在 datagridview 控件中选择多个单元格?
use the MultiSelect
property
edit: depending on what you want to accomplish, you might have to also use the SelectionMode
property
If you are looking to prevent control clicks from multiple columns you can do the following:
private int nSelectedColumn = 0;
(add a CellClick event on the datagridview and copy in the following code:)
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
{
if (_nSelectedColumn != 0)
{
if (_nSelectedColumn != e.ColumnIndex)
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = false;
if (Control.ModifierKeys == Keys.Shift)
dataGridView.ClearSelection();
}
}
else
_nSelectedColumn = e.ColumnIndex;
}
else
_nSelectedColumn = e.ColumnIndex;
}
Also ensure multiselect is enabled and SelectionMode is set to CellSelect.
The effect is that you can control click items in a column and even shift click in the same column. The cell will deselect when you control click outside of that column and if you shift click outside of the column it will deselect everything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.