Data Grid Column Header Text Wrapping

Posted by: msloan on 9 April 2018, 10:31 pm EST

    • Post Options:
    • Link

    Posted 9 April 2018, 10:31 pm EST

    Hi,

    For the WPF Data Grid control it is possible to programmatically turn on Text Wrapping for the rows of data:

    foreach (var L_objColumn in M_dgComOne.Columns)
    {
                // Allow Text to wrap
                ((C1.WPF.DataGrid.DataGridBoundColumn)L_objColumn).TextWrapping = System.Windows.TextWrapping.Wrap;
    }
    

    How can I do the same for the Column Headers? The Columns are auto-generated by assigning a Data table created from an SQL Query to the Data Grid’s Item Source:

                 M_dgComOne.ItemsSource = M_datGridDataTable.AsDataView();
    

    Therefore I can’t implement Text Wrapping in the XAML files.

    Any help appreciated.

  • Posted 10 April 2018, 6:10 pm EST

    Hello Mark,

    In that case, you may use the AutoGeneratingColumn event of the grid columns and set the column header to a TextBlock with it’s TextWrapping property set to Wrap, as follows:```

    private void _dataGrid_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)

    {

    TextBlock tbHeader = new TextBlock();

    tbHeader.TextWrapping = TextWrapping.Wrap; //for wrapping the Header

    tbHeader.Text = e.Column.Header.ToString();

    tbHeader.SizeChanged += TbHeader_SizeChanged;

    [b]e.Column.Header = tbHeader;[/b]
    

    }

    private void TbHeader_SizeChanged(object sender, SizeChangedEventArgs e)

    {

    if (e.NewSize.Height > _dataGrid.ColumnHeaderHeight)

    {

    _dataGrid.ColumnHeaderHeight = e.NewSize.Height + 5;

    }

    }

    Also, a sample application has been attached with this thread for you to refer.
    
    Regards,
    Ruchir Agarwal
    [zip filename="Wrap_ColumnHeader.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-13046cca-17a7-4bcc-adaf-3b6fea917894.zip[/zip]
  • Posted 10 April 2018, 9:22 pm EST

    Hi,

    Your example project works fine, but from some reason in my project I’m seeing large Height values of over 400 & 500 in TbHeader_SizeChanged, leading to:

    Any ideas?

  • Posted 10 April 2018, 9:37 pm EST

    UPDATE I logged all the calls to the Size Changed Event Handler and I seem to be getting two sets of calls the first with large values and the second with the expected values:

    2018-04-11 11-33-15.446|INFO||: e.NewSize.Height = 425.6 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.448|INFO||: e.NewSize.Height = 383.04 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.449|INFO||: e.NewSize.Height = 574.56 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.451|INFO||: e.NewSize.Height = 255.36 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.453|INFO||: e.NewSize.Height = 234.08 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.454|INFO||: e.NewSize.Height = 255.36 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.456|INFO||: e.NewSize.Height = 170.24 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.639|INFO||: e.NewSize.Height = 42.56 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.644|INFO||: e.NewSize.Height = 21.28 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.646|INFO||: e.NewSize.Height = 85.12 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.657|INFO||: e.NewSize.Height = 63.84 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.659|INFO||: e.NewSize.Height = 21.28 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.661|INFO||: e.NewSize.Height = 42.56 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    2018-04-11 11-33-15.662|INFO||: e.NewSize.Height = 21.28 (System.Windows.Controls.TextBlock)|TbHeader_SizeChanged --- InvokeEventHandler --- InvokeHandler --- InvokeHandler|
    
    

    So I have changed the Event Handler to ignore Sizes over 100 and this has the desired effect.

  • Posted 11 April 2018, 7:17 pm EST

    Hi Mark,

    Thank you for confirming that the approach works fine with our sample.

    Re. it not working in your application: From the information you shared, it looks as if the grid columns are generating multiple times in your application and hence triggering the SizeChanged event. However, in order to find the cause for this or suggest any workaround other than the one you already found, we would require to have a look at your solution once.

    So, we request you to please share your application or a stripped version of your application replicating the issue.

    If you have some issues sharing the application here publicly then you may do the same privately using our portal: https://supportone.componentone.com/login

    Regards,

    Ruchir Agarwal

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels