Wouter Verheyen
Wouter Verheyen

Reputation: 33

How to dynamically bind datatable to grid and wrap data in certain columns?

I've searched around for a while and there are lots of posts on this topic, but none seem to give me the right answer.

I've created a datagrid on my form simply as:

 <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="860">
      <DataGrid ScrollViewer.VerticalScrollBarVisibility="Auto" 
       ScrollViewer.HorizontalScrollBarVisibility="Auto" 
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Top" 
       Name="DGI" 
       Height="700" 
       ItemsSource="{Binding}"
       Background="LightGray" RowBackground="LightYellow" AlternatingRowBackground="LightBlue"> 
      </DataGrid>
 </StackPanel>

Now I bind the data in my code as follows:

private void btnUpdateGridI_Click(object sender, RoutedEventArgs e)
    {
        DGI.DataContext = null;
        IEnumerable<DataRow> query =
            from punch in dspl.Tables[0].AsEnumerable()
            where punch.Field<String>("TOS").Contains(cmbTOSI.SelectedItem.ToString()) &&
                punch.Field<String>("BU").Contains(cmbBUI.SelectedItem.ToString()) &&
                punch.Field<String>("CLOSED").Contains(cmbClosedI.SelectedItem.ToString()) &&
                punch.Field<String>("CAT").Contains(cmbCATI.SelectedItem.ToString()) 
            select punch;
        try
        {
            DataTable boundTable = query.CopyToDataTable<DataRow>();
            DGI.DataContext = boundTable;
            lbltotalitemsI.Content = boundTable.Rows.Count.ToString() + " ITEMS";
            DGI.Columns[6].MaxWidth = 350;
        }
        catch
        {
            MessageBox.Show("No data exists for the current selection.");
            lbltotalitemsI.Content = "0 ITEMS";
            DGI.DataContext = null;
        }
    }

I'm trying to make the text in Column 6 wrap. I was able to define its maximum width, but for wrapping the text most online sources refer to the use of a TextBlock in the datagrid.

Is there an easy way of doing this dynamically? I would be wanting to do this for multiple columns.

Upvotes: 1

Views: 1153

Answers (1)

yo chauhan
yo chauhan

Reputation: 12295

Hi No we can't set TextWrap directly that is why Templateing is provided (Template is responsible for appearance without affecting the behavior of control). A short example for above problem is

    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True">//Set your all properties as you want
    <DataGrid.Columns>
        <DataGridTextColumn Header="TOS" Binding="{Binding TOS}"></DataGridTextColumn>
        <DataGridTextColumn Header="BU" Binding="{Binding BU}"></DataGridTextColumn>
        <DataGridTextColumn Header="CLOSED" Binding="{Binding CLOSED}"></DataGridTextColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <!--This is how we can set Text Wrap-->
                    <TextBox Text="{Binding CAT}" TextWrapping="Wrap"/> 
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Column 4 is TextWrap. I hope this will help.

Upvotes: 1

Related Questions