i know nothing
i know nothing

Reputation: 1002

WPF DataGrid: Making an entire column unselectable

I have a DataGrid which holds a few columns, some of them shouldn't be selectable to the user (since they're read-only anyway). There is no property for the column itself, apparently I need to handle this through the SelectedCellsChanged-Event.

I can use an IF-statement to find out whether the corresponding column of a cell is non-selectable by doing something like this:

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    if (e.AddedCells[0].Column.Header.ToString() == "Non Selectable Column")
        {
            // What now?
        }
}

How do I keep the cells in this collection from getting selected, though?

Upvotes: 1

Views: 1624

Answers (1)

i know nothing
i know nothing

Reputation: 1002

Alright, I got it. It may not be the perfect solution, but it works flawlessly for me. Even with lots of cells AND when selecting cells from several columns containin ones that should be selectable. :)

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach (DataGridCellInfo cell in e.AddedCells)
    {
        if (cell.Column.Header.ToString() == "NonSelectableColumn")
        {
            MyDataGrid.SelectedCells.Remove(cell);
        }
    }
}

Upvotes: 2

Related Questions