Search code examples
c#wpfdatagrid

How can I show an ellipsis in a DataGrid if the content of a cell is greater than its width?


Is it possible to show ... if the content of some cell in a DataGrid is greater than the specified ColumnWidth? Something like:

-------------------------------
|   Name   |    Last Name     |
-------------------------------
| LongNa.. |                  |
-------------------------------

Solution

  • TextTrimming is what you are looking for. Use the DataGridTemplateColumn and insert a TextBlock into the CellTemplate which supports TextTrimming. Sample -

    <DataGrid ItemsSource="{Binding ItemsSourceForYourGrid}">
       <DataGrid.Columns>
          <DataGridTemplateColumn Width="20">
             <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis"/>
                </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>