开发者

Prevent multiple cells from being selected in DataGridView Control

Can anyone tell me how to prevent multiple cells from being selected in datagridview co开发者_StackOverflowntrol?


use the MultiSelect property

edit: depending on what you want to accomplish, you might have to also use the SelectionMode property


in the dataGridView properties >> Behavior section >> set MultiSelect to false

Prevent multiple cells from being selected in DataGridView Control


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜