none
datagrid select but not edit

    Question

  • i notice that first click on a cell selects the row, 2nd click selects the cell and goes into edit mode.  i want to be able to copy/past from a cell so in edit mode i can, but problem is people can accidently edit the cell by accident and then copy the corrupted data.  if i set the datagrid as readonly, then i can't copy the cell anymore since i can't go into edit mode.  any suggestions?

    also it would be cool if we are able to select a whole row, or group of rows, or entire column or group of columns like we can in excel and be able to paste that data directly to excel.  just a suggestion for final release :)

    Saturday, June 7, 2008 3:34 PM

Answers

  • Hi:

      You can use DataGridTemplateColumn and readonly TextBox to do this:

      <my1:DataGrid x:Name="DataGrid1" Width="500" Height="500" AutoGenerateColumns="False" >
                <my1:DataGrid.Columns>
                    <my1:DataGridTemplateColumn Width="80">
                        <my1:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <StackPanel>

                                    <TextBox IsReadOnly="True" Text="{Binding age}"></TextBox>
                                </StackPanel>
                            </DataTemplate>
                        </my1:DataGridTemplateColumn.CellTemplate>
                    </my1:DataGridTemplateColumn>
                </my1:DataGrid.Columns>
            </my1:DataGrid>

    Regards

    Tuesday, June 10, 2008 2:02 AM
  • You would do it like this:

    <data:DataGrid Height="600" x:Name="dataGrid">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn DisplayMemberBinding="{Binding ID}" Header="ID">
                <data:DataGridTextColumn.EditingElementStyle>
                    <Style TargetType="TextBox">
                        <Setter Property="IsReadOnly" Value="true" />
                    </Style>
                </data:DataGridTextColumn.EditingElementStyle>
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

    That sets the style on the EditingElement which is the TextBox in this case.  The the style then sets TextBox's IsReadOnly property to true.

    Thursday, June 12, 2008 3:23 PM

All replies

  • Hi:

      You can use DataGridTemplateColumn and readonly TextBox to do this:

      <my1:DataGrid x:Name="DataGrid1" Width="500" Height="500" AutoGenerateColumns="False" >
                <my1:DataGrid.Columns>
                    <my1:DataGridTemplateColumn Width="80">
                        <my1:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <StackPanel>

                                    <TextBox IsReadOnly="True" Text="{Binding age}"></TextBox>
                                </StackPanel>
                            </DataTemplate>
                        </my1:DataGridTemplateColumn.CellTemplate>
                    </my1:DataGridTemplateColumn>
                </my1:DataGrid.Columns>
            </my1:DataGrid>

    Regards

    Tuesday, June 10, 2008 2:02 AM
  • DataGridColumn has a IsReadOnly property.  Set this to true for any column that you don't want to be editable

    Tuesday, June 10, 2008 4:47 PM
  • if i set it to isreadonly, then the cell is no longer selectable and i can't copy/paste from it.

    Wednesday, June 11, 2008 12:39 AM
  •  Hi:

    if i set it to isreadonly, then the cell is no longer selectable and i can't copy/paste from it.

      Have you tried my suggestion?

    Regards

    Wednesday, June 11, 2008 12:40 AM
  • Ah ok, I see.  You can leave column.IsReadOnly = false, and set the column.EditingElementStyle on the column to a Style that sets the IsReadOnly property to true.  This will set the IsReadOnly property on the TextBox to true so you can get the behavior you want.  Alternatively, you can do what Alan suggested.

    Wednesday, June 11, 2008 12:50 AM
  • Hi Allen, I tried your suggestion like below.  where i use <data:DataGridTemplateColumn Header="ID"> it seems to work as i would like.  but i don't get the nifty builtin sort that comes with beta2.  but if i do it like <data:DataGridTextColumn DisplayMemberBinding="{Binding ID}"> then i get the cool column sorting but i can't figure out how to set the column.EditingElementStyle like yifung mentions.  i think he is referring to codebehind and using an event to modify the property of the edit cell but i'm not sure how to do that or which event to trigger it and where to put the trigger.  sorry i'm still a newbie to all this.  any help with as much detail is greatly appreciated :)

                    <data:DataGridTemplateColumn Header="ID">
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding ID}" />
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                        <data:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding ID}" IsReadOnly="True" />
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellEditingTemplate>
                    </data:DataGridTemplateColumn>

                    <data:DataGridTextColumn DisplayMemberBinding="{Binding ID}">
                        <data:DataGridTextColumn.Header>
                            <TextBlock Text="ID" Style="{StaticResource grid-header}" />
                        </data:DataGridTextColumn.Header>
                    </data:DataGridTextColumn>

    Thursday, June 12, 2008 5:42 AM
  • You would do it like this:

    <data:DataGrid Height="600" x:Name="dataGrid">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn DisplayMemberBinding="{Binding ID}" Header="ID">
                <data:DataGridTextColumn.EditingElementStyle>
                    <Style TargetType="TextBox">
                        <Setter Property="IsReadOnly" Value="true" />
                    </Style>
                </data:DataGridTextColumn.EditingElementStyle>
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

    That sets the style on the EditingElement which is the TextBox in this case.  The the style then sets TextBox's IsReadOnly property to true.

    Thursday, June 12, 2008 3:23 PM
  • that worked!!  thank you

    Thursday, June 12, 2008 7:19 PM
  • hi,

     If we have datagrid with AutoGernerateColumn property true, in that case how to make datagrid cell text readonly?

    for example i have following code in xaml.   

    <data:DataGrid           x:Name="CashResultGrid"  >

     and in code behind i am setting  its value

     CashResultGrid.ItemSource = MyObject

     My code working fine, it is displaying Property of MyObject as header and data as row of CashResultGrid, but i want row of my CashResultGrid need to be ReadOnly.

     User can not modify row data.

    Wednesday, May 27, 2009 1:20 AM
  • I'm also interested about dynamically changing TextBox readonly property in DataGrid. Best way seems to bing TextBox readonly property to viewModel. If DataGrid DataContext is not viewmodel property , how to add binding to separate viewmodel object ?

    Tuesday, October 6, 2009 7:54 AM
  • <data:DataGrid IsReadOnly="True" ... />


    Cheers

    Balaji Ganesan

    Friday, April 29, 2011 8:16 AM
  • I found

    <data:DataGrid IsReadOnly="True" ... />

    or

    <data:DataGrid IsReadOnly="False" ... />

    as the simplest solution, besides other possible solutions discussed. Many thanks Balaji.

    Tuesday, December 27, 2011 9:52 PM
  • it's work for me. Thank! :D
    Thursday, October 18, 2018 7:32 AM
  • When I try to manipulate the cells quickly, i can edit it, it shows an editable error, shows an error message and locks all the remaining cells that can not be selected.

    Thursday, October 18, 2018 7:35 AM