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.
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
Column 4 is TextWrap. I hope this will help.