How can I wrap Text in a DataGridTextColumn - C#-Code

Apr 5, 2009 at 8:18 PM
Hallo!

Sorry for the Beginner-Question!
I create a datagrid in my C#-Code and generate the columns (no autogenerate). But I can't get the textwrapping working! :(

What I'm doing wrong?

               // DataView dv;
               // double[] columnWidth;

                DataGrid dg = new DataGrid();
                dg.Height = TSS_cReport.FromCentimeter(Height);
                dg.Width = TSS_cReport.FromCentimeter(Width);

                DataGridTextColumn dgtc;
                for (int i = 0; i < columnWidth.Length; i++)
                {
                    dgtc = new DataGridTextColumn();
                    dgtc.Width = TSS_cReport.FromCentimeter(columnWidth[i]);  // Set width
                    dgtc.MaxWidth = TSS_cReport.FromCentimeter(columnWidth[i]);  // Set MaxWidth
                    dgtc.Binding = new Binding(dv.Table.Columns[i].ColumnName); // Set Binding to DataView-Column
                    dgtc.Header = dv.Table.Columns[i].Caption;  // Set Header
                    dgtc.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); // Try to activate the Text-Wrapping !!! :(
                    dg.Columns.Add(dgtc);  // Add column to the DataGrid
                }

                dg.RowHeight = 50;
                dg.CanUserResizeRows = true;
                dg.EnableRowVirtualization = true;
                dg.AutoGenerateColumns = false;
                dg.ItemsSource = dv;  // DataView
                dg.MaxWidth = TSS_cReport.FromCentimeter(Width);  // MaxWidth of the DataGrid


I can't use XAML in this case, because I use this DataGrid for printing.

Thanks for your help!
Tucca
Coordinator
Apr 6, 2009 at 12:33 PM
Try something like this:

 

var style = new Style(typeof(TextBlock));
style.Setters.Add(
new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));
dgtc.ElementStyle = style;

 

Apr 6, 2009 at 2:41 PM
It work's !!! :)

Thanks!
Tucca