Miko Kronn
Miko Kronn

Reputation: 2454

DataGridView: How to make some cells unselectable?

How can I make some cells in DataGridView unselectable?

By 'unselectable' I mean: It cannot be selected in any way and trying to select it won't unselect any other cell.

I don't mean ReadOnly. My cells already have this property as true.

DataGridView.MultiSelect needs to be false.

Thanks to JYL's answer I wrote a code:

    private int selectedCellRow = 0;
    private int selectedCellColumn = 0;

    private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
    {
        if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
                return;

        if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
        {
            e.Cell.Selected = false;
            grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
        }
        else
        {   
            selectedCellRow = e.Cell.RowIndex;
            selectedCellColumn = e.Cell.ColumnIndex;
        }

        //this was only for seeing what is happening
        //this.Text = selectedCellRow + " " + selectedCellColumn;
    }

But this leads to StackOverflow. What condition and where I need to put to prevent that?

Upvotes: 23

Views: 23546

Answers (3)

Ichibann
Ichibann

Reputation: 4451

Added and commented the condition you were asking about.

private int selectedCellRow = 0;
private int selectedCellColumn = 0;

private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
    if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
        return;

    //if Cell that changed state is to be selected you don't need to process
    //as event caused by 'unselectable' will select it again
    if (e.Cell.RowIndex == selectedCellRow && e.Cell.ColumnIndex == selectedCellColumn)
        return;

    //this condition is necessary if you want to reset your DataGridView
    if (!e.Cell.Selected)
        return;

    if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
    {
        e.Cell.Selected = false;
        grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
    }
    else
    {
        selectedCellRow = e.Cell.RowIndex;
        selectedCellColumn = e.Cell.ColumnIndex;
    }       
}

Upvotes: 16

JYL
JYL

Reputation: 8319

You can use the event "CellStateChanged".

private void DataGridViewXYZ_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
                if (e.Cell == null
                    || e.StateChanged != DataGridViewElementStates.Selected)
                    return;
                if (! [condition here : can this cell be selectable ?])
                    e.Cell.Selected = false;
}

EDIT : if you leave the MultiSelect property of gridView to True, you can manage yourself a "single select" gridview with unselectable cells : il the cell is selectable, clear the other selection...

Upvotes: 6

THE DOCTOR
THE DOCTOR

Reputation: 4555

I believe this article may prove useful to you:

http://blog.spencen.com/2009/04/25/readonly-rows-and-cells-in-a-datagrid.aspx

The ReadOnly property can be applied to the entire grid, a column, a row, or an individual cell.

Upvotes: 1

Related Questions