Multiline in SfDataGrid

I have a SfDataGrid, usually data is single line, but there are some rows that can have multiple lines. However it seems the line height is being set by first line and multiline text getting trimmed. How can I make SfDataGrid to auto set the height of every row.

I'm using version 18.4.0.30.

Thank you.


7 Replies

MA Mohanram Anbukkarasu Syncfusion Team January 12, 2022 12:41 PM UTC

Hi John, 

You can achieve your requirement to auto fit the row height based on its content by handling QueryRowHeight. Please refer the below given help documentation.  


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 



JO John replied to Mohanram Anbukkarasu January 13, 2022 04:45 PM UTC

I use QueryRowHeight event now but it still trims some of cell content, like 2 or 3 pixels. Also it ruins the Row Header, I specified the HeaderRowHeight but it seems to be ignoring it now.


GridRowSizingOptions gridRowResizingOptions = new GridRowSizingOptions();

//To get the calculated height from GetAutoRowHeight method.
double autoHeight;
this.dataGrid.QueryRowHeight += dataGrid_QueryRowHeight;

void dataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
{
    if (this.dataGrid.GridColumnSizer.GetAutoRowHeight(e.RowIndex, gridRowResizingOptions, out autoHeight))
    {
        if (autoHeight > 24)
        {
            e.Height = autoHeight;
            e.Handled = true;
        }
    }
}


JO John January 13, 2022 04:48 PM UTC

Okay if I add following code, the header is back to normal.



if (e.RowIndex == 0)
                return;


Turn out, the Row 0 is header, thats not good idea.



JO John January 13, 2022 05:02 PM UTC

how do I get Row's source object inside QueryRowHeight


SJ Sathiyathanam Jeyakumar Syncfusion Team January 14, 2022 09:46 AM UTC

Hi John,


You can get the Row Data (ie. Source object of the Row) inside the QueryRowHeight event by using the RowIndex.


//Here I have used the row source object as EmployeeInfo which is getting from record entry.Data.

 

var recordEntry = this.sfDataGrid.View.Records[e.RowIndex] as RecordEntry;

if (recordEntry != null && recordEntry.Data != null)

{

     var contactNumber = (recordEntry.Data as EmployeeInfo).ContactNumber;

}


Sample: https://www.syncfusion.com/downloads/support/forum/171888/ze/DataGridDemo-217476072.zip


Regards,

Sathiyathanam



JO John replied to Sathiyathanam Jeyakumar January 15, 2022 02:16 AM UTC

okay but there is another problem the dataGrid.GridColumnSizer.GetAutoRowHeight() always outputs same value for all rows



Edit :

Following error on var recordEntry = this.sfDataGrid.View.Records[e.RowIndex] as RecordEntry;
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'


Are you aware, row index 0 is row header ?



Edit 2 :

Even the DataGrid "AutoRowHeightDemo" doesn't work properly, to test, open that demo and change the combobox value to something longer value and you will see the selected value getting trimmed.




MA Mohanram Anbukkarasu Syncfusion Team January 17, 2022 02:43 PM UTC

Hi John,  

We regret to inform that we are little unclear about the problem you are reporting about GetAutoRowHeight. If possible, please revert to us with more details about the issue along with a video illustration. It will be more helpful for us to understand the exact issue and to provide a proper solution.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon