I
I
idShura2017-01-13 09:20:27
WPF
idShura, 2017-01-13 09:20:27

How to wrap text in datagrid cells?

I'm trying to make text wrap to a new line depending on the width of the column in the datagrid. It worked, but the cells show System.Data.DataRowView . What am I doing wrong?
I load the data into the table from the database as follows:
dataGrid1.ItemsSource = r2.ResultData.DefaultView;

<Window.Resources>
    <DataTemplate x:Key="templ">
        <TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" />
    </DataTemplate>
</Window.Resources>

<Grid>
    <DataGrid   Name="dataGrid1" 
                DockPanel.Dock="Left"  
                SelectionUnit="FullRow" 
                SelectionMode="Single" 
                HeadersVisibility="Column" 
                IsReadOnly="True" 
                AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" >
    </DataGrid>
</Grid>

private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header.ToString() == "NAME_DOC")
    {
        e.Column.Header = "Наименование";
       
       // ...

        Style style = new Style(typeof(DataGridCell));
        style.Setters.Add(new Setter(DataGridCell.ContentTemplateProperty, Resources["templ"]));
        e.Column.CellStyle = style;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2017-01-13
@idShura

I figured it out myself, in Binding I still had to specify the name of the column.

<TextBlock Text="{Binding ИМЯ_КОЛОКИ}" TextWrapping="Wrap" Foreground="Black" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question