120

I'm constructing a datagrid in Windows Presentation Foundation, and I have a problem. When a user double-clicks on a cell in my datagrid, the cell goes into edit mode. I want to prevent that. Instead I want users to be able to select the full row - not edit values in it.

How can I make it so that double-clicks select the full row instead of putting the clicked-on cell in edit mode?

Brighid McDonnell
  • 4,109
  • 3
  • 34
  • 59
Pouyan
  • 2,499
  • 7
  • 27
  • 37

4 Answers4

272

The WPF DataGrid has an IsReadOnly property that you can set to True to ensure that users cannot edit your DataGrid's cells.

You can also set this value for individual columns in your DataGrid as needed.

STiLeTT
  • 925
  • 10
  • 23
Leslie Davies
  • 3,772
  • 1
  • 14
  • 14
  • 1
    Grid is grayed and looks more like in a disabled state - is not resembling with the readonly state from the winforms. – Buda Florin Jun 03 '16 at 06:17
  • 3
    I agree with Buda Florin. Setting it to ReadOnly makes it look more like it is disabled. Disabled and ReadOnly are different because ReadOnly normally lets you select the text for copying. When you set the DataGrid to be ReadOnly you can no longer select text in the cells. – Nick Oct 05 '16 at 00:04
  • 2
    @LeslieDavies what about if I want to keep them disabled, but I also want to be able to remove items from datagrid when I press DEL? – Roxy'Pro Mar 06 '17 at 09:47
  • @Roxy'Pro, I have the same question. Did you find a solution? – Patrick Sep 12 '18 at 11:00
46

The DataGrid has an XAML property IsReadOnly that you can set to true:

<my:DataGrid
    IsReadOnly="True"
/>
newfurniturey
  • 34,078
  • 9
  • 85
  • 99
Stephen
  • 461
  • 4
  • 2
4

If you want to disable editing the entire grid, you can set IsReadOnly to true on the grid. If you want to disable user to add new rows, you set the property CanUserAddRows="False"

<DataGrid IsReadOnly="True" CanUserAddRows="False" />

Further more you can set IsReadOnly on individual columns to disable editing.

VivekDev
  • 7,143
  • 11
  • 58
  • 107
2

I see users in comments wondering how to disable cell editing while allowing row deletion : I managed to do this by setting all columns individually to read only, instead of the DataGrid itself.

<DataGrid IsReadOnly="False">
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="True"/>
        <DataGridTextColumn IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>
Célia
  • 21
  • 2