Dynamically implement line breaks in Datagrid

Hi.

I am implementing some details in the application grid. And the need arose to break lines according to the content. Searching the documentation I found the link (https://help.syncfusion.com/wpf/datagrid/row-height-customization#calculate-height-based-on-certain-columns). Everything is working, but in AssociatedObject_ItemsSourceChanged method on the QueryRowHeightBehaviour helper I have to pass the columns that I want to wrap the line, but I'm setting AutoGenerateColumns to true in my SfDataGrid. I would like to take these columns dynamically in the method below. I made several attempts, but to no avail. Is there a simple way to do this? What?

void AssociatedObject_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
{           
     foreach (var column in this.AssociatedObject.Columns)
          if (!column.MappingName.Equals("Address") && !column.MappingName.Equals("CompanyName"))
               excludeColumns.Add(column.MappingName);

     gridRowResizingOptions.ExcludeColumns = excludeColumns;
}


4 Replies

VS Vijayarasan Sivanandham Syncfusion Team February 13, 2020 02:14 PM UTC

Hi David Pressman, 
 
Thank you for contacting Syncfusion Support. 
 
We have analyzed your query and we found that you have loaded the column in itemsource changed   event. So only the text line could not wrap. Your requirement can be achieved by using the SfDataGrid.AutoGeneratingColumn instead of ItemSource event.  
 
 
 private void Datagrid_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingColumnArgs e) 
        { 
            if (!(e.Column.MappingName == "Symbol") && !(e.Column.MappingName == "PreviousClose")) 
                excludeColumns.Add(e.Column.MappingName); 
            else 
                (e.Column as GridTextColumn).TextWrapping = TextWrapping.Wrap; 
 
           gridRowResizingOptions.ExcludeColumns = excludeColumns; 
        } 
 
 
Please find the following image for your reference, 
 
 
 
 
 
 
Please let us know, if you need any further assistance on this. 
 
Regards, 
Vijayarasan


TS Tech Shop February 13, 2020 05:01 PM UTC

Hi

The example that has passed me has not yet reached where I need it. Below I describe better what I need. Because I looked at the links and examples sent and my question still remains.

In DataGrid_AutoGeneratingColumn you are statically defining the two columns "Symbol" and "PreviousClose". I want it not to be necessary to pass the columns. In my scenario I will have several columns, as they will be generated dynamically in my DataGrid. My DataGrid is being shared on several screens, there are moments that load 5 columns and other times more or less columns. In my scenario, if the column is of the GridTextColumn type, it should always break a line of text, if the text is greater than the width of the column. And then the height of the line adjusts to the content.

In my code example, replace "Symbol" and "PreviousClose" with "Field1" and "Field2". But I don't want to have to pass the fields that will have a line break.

namespace TechPosto.Library.Presentation.Helpers
{

    using Syncfusion.UI.Xaml.Grid;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Interactivity;

    public class QueryRowHeightBehaviour : Behavior
    {
        GridRowSizingOptions gridRowResizingOptions = new GridRowSizingOptions();
        List excludeColumns = new List();
        double Height = double.NaN;

        protected override void OnAttached()
        {
            this.AssociatedObject.QueryRowHeight += AssociatedObject_QueryRowHeight;
            this.AssociatedObject.AutoGeneratingColumn += AssociatedObject_AutoGeneratingColumn;
        }

        void AssociatedObject_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
        {

            if (!(e.Column.MappingName == "Campo1") && !(e.Column.MappingName == "Campo2"))
                excludeColumns.Add(e.Column.MappingName);
            else
                (e.Column as GridTextColumn).TextWrapping = TextWrapping.Wrap;

            gridRowResizingOptions.ExcludeColumns = excludeColumns;
        }

        void AssociatedObject_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
        {

            if (this.AssociatedObject.IsTableSummaryIndex(e.RowIndex))
            {
                e.Height = 24;
                e.Handled = true;
            }
            else if (this.AssociatedObject.GridColumnSizer.GetAutoRowHeight(e.RowIndex, gridRowResizingOptions, out Height))
            {
                if (Height > this.AssociatedObject.RowHeight)
                {
                    e.Height = Height;
                    e.Handled = true;
                }
            }
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.QueryRowHeight -= AssociatedObject_QueryRowHeight;
            this.AssociatedObject.AutoGeneratingColumn -= AssociatedObject_AutoGeneratingColumn;
        }
    }
}



TS Tech Shop February 14, 2020 12:53 PM UTC

Hi

Thank you for your help. But I managed to solve my problem using annotations in my proxy. Now it's dynamic! I don't need to pass which column, I just define it in my proxy.


FP Farjana Parveen Ayubb Syncfusion Team February 14, 2020 12:59 PM UTC

Hi Tech Shop, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Farjana Parveen A 


Loader.
Up arrow icon