0

I have an editable WPF DataGrid bound to DataTable, and data loaded from SQL Server page by page using ADO.NET. There are some text fields with type nvarchar(MAX). So the user should be able to edit and save the text inside the DataGrid. All this works well with normal DataGridTextColumns. But it is very inconvenient for the user, therefore I want to show 2 lines visible in the text column and a vertical scroll bar to scroll the remaining.

I tried following both DataGridTextColumn and DataGridTemplateColumn.

Using DataGridTemplateColumn it shows the way we want, but when edit it does not appear in DataSet has changes. (This is required to warn user while navigating through pages) Therefore it doesn’t save the changes.

<DataGridTemplateColumn Header="Full Description" Width="*">
     <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                 <TextBox x:Name="dgFullDescription" 
                          Text="{Binding FullDescription, Mode=TwoWay}" 
                          TextWrapping="WrapWithOverflow" Height="36" 
                          VerticalScrollBarVisibility="Visible" />
            </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

On the otherhand DataGridTextColumn does save changes but couldn’t show scroll bar. If I set the text wrapping then it shows more than 2 lines if it has more characters, and therefore row height are inconsistent.

<DataGridTextColumn Header="ShortDescription" Width="*" 
                    Binding="{Binding ShortDescription, Mode=TwoWay}"  >
        <DataGridTextColumn.ElementStyle>
              <Style TargetType="TextBlock">
                     <Setter Property="TextWrapping" Value="Wrap" />
                     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" 
                             Value="Visible" />
               </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.EditingElementStyle>
                <Style TargetType="TextBox">
                      <Setter Property="TextWrapping" Value="Wrap" />
                      <Setter Property="HorizontalScrollBarVisibility" 
                              Value="Visible" />
                </Style>
        </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

How can I set the vertical scrollbar inside editable Text Column with fixed row height in WPF DataGrid ?

4
  • 1) are you setting the height ? 2) I'm not sure about how how the element is treating it's inner child. You can have a look at (this article)[codeproject.com/Tips/659066/ListBox-and-Panels-in-WPF], specifically the 2nd example, where the scrollbar won't show. It might give you a hint. Commented Aug 20, 2014 at 21:21
  • @Noctis , 1).Setting height does limit the height but it doesn't show the vertical scroll bar. user can't see the entire text without entering in to edit mode. Commented Aug 20, 2014 at 22:43
  • @Noctis, 2)Link didn't help for this issue. Commented Aug 20, 2014 at 22:45
  • Sorry to hear. I'm not currently in front of a WPF project so can't do much from memory. I'm sure someone else will help though Commented Aug 20, 2014 at 23:24

1 Answer 1

0

Finally I was able to get it work with DataGridTemplateColumn. Both vertical scroll bar and fixed height works well with it. Thanks @Noctis for your advice regarding the height. To get the changes made to the textbox inside DataGridTemplateColumn to be available in the DataSet changes for update, I have to add UpdateSourceTrigger=LostFocus to text binding. The final code as follows:

<DataGridTemplateColumn Header="Full Description" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
             <TextBox x:Name="dgFullDescription" 
                      Text="{Binding FullDescription, Mode=TwoWay, UpdateSourceTrigger=LostFocus }" 
                      TextWrapping="WrapWithOverflow" Height="36" 
                      VerticalScrollBarVisibility="Visible" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.