We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

dataGrid.GetRowHeight(e.RowIndex) returns always '0'

I am using CustomCells for my SfDataGrid.

I want to Autofit the grid rows based on content  but unfortunately
dataGrid.GetRowHeight(e.RowIndex);
always returns '0', so my Grid is "empty" when using DataGrid_QueryRowHeight

Do i have to do any special when working with CustomCells?
Please let me know if I should provide more code :)



6 Replies

PK Pradeep Kumar Balakrishnan Syncfusion Team January 15, 2020 10:02 AM UTC

Hi Frank , 
 
Thank you for contacting Syncfusion support.. 
  
We have checked your query “How to measure row height for custom cell (Template column) in Xamarin Android.”. We don’t have support to measure datagrid custom cell because it's not possible to find out what kind of view custom cell contains. However we can define height to custom cell in sample level by overriding GetAutoHeight() method in your custom view please refer the following code snippet for reference. 
 
Code Snippet: 
public class CustomCell : GridCell 
{ 
    TextView textView; 
 
    public CustomCell(Context context) : base(context) 
    { 
        textView = new TextView(this.Context); 
        this.AddView(textView); 
        this.CanRenderUnLoad = false; 
    } 
 
    protected override void UnLoad() 
    { 
        if (this.Parent != null) 
            (this.Parent as VirtualizingCellsControl).RemoveView(this); 
    } 
 
    protected override void OnLayout(bool change, int l, int t, int r, int b) 
    { 
        this.textView.Layout(0, 0, this.Width, this.Height); 
    } 
 
    protected override void OnDraw(Canvas canvas) 
    { 
        base.OnDraw(canvas); 
        this.textView.Text = DataColumn.CellValue.ToString(); 
    } 
 
    protected override double GetAutoHeight(object rowData) 
    { 
        string cellValue = (rowData as OrderInfo).CustomerID; 
        // return the desired value. 
        return base.GetAutoHeight(rowData); 
    } 
} 
 
Please let us know if you need further assistance on this. 
  
Regards, 
Pradeep Kumar B 



FE Frank Effenberger January 15, 2020 01:48 PM UTC

Hi Pradeep,
thanks for your response.

Meanwhile, I am using a DummyView with same content as the cell and calculate the max height of all cells per row. The height calculation works fine so far,
but I also encountered the next problem:

DataGrid_QueryRowHeight is not called for every row, when changing the ColumnWidth. For example my Grid has 13 items (which are all in the view), and the Event is only triggered for row 1 and row 13 (QueryRowHeightEventArgs e -> e.RowIndex). This leads to the result that only the height of row 1 and row 13 are changed.

Current Workaround:
_sfGrid.ColumnResizing += (sender, e) =>
{
      _sfGrid.Refresh();
 };

but this leads to a bad performance


EDIT:
Even if I work with a normal GridTextColumn and  using

private void DataGrid_QueryRowHeight (object sender, QueryRowHeightEventArgs e)
{
    if (e.RowIndex != 0) {
        //Calculates and sets height of the row based on its content 
        e.Height = dataGrid.GetRowHeight(e.RowIndex);
        e.Handled = true;
    }
}

the Height of the Cells is not updates correctly




FE Frank Effenberger January 16, 2020 07:11 AM UTC

Sample project

I just created a simple sample project: 1000 rows, 7 items each, normal GridTextColumn

In additon to poor scrolling performance (compared to a normal recycler view) you can also see the bug mentioned.
Just drag the divider of Field01 and Field02 to the left so that the Field01-Cells needs a line break. Only the first row height ist adjusted.


(maybe 30MB upload limit is a little bit outdated :p )


PK Pradeep Kumar Balakrishnan Syncfusion Team January 17, 2020 12:43 PM UTC

Hi Frank Effenberger, 
 
Thank you for the update. 
 
Query 1: “Only first and last visible row height is refreshed after resizing” 
 
Query row height will be raised while creating new row or updating the row in scrolling. Refresh the grid after resizing action is completed it will not lead to bad performance  
 
Code snippet: 
private void SfDataGrid_ColumnResizing(object sender, GridResizingEventArgs e) 
{ 
    if (e.ResizingState == ProgressStates.Completed) 
        sfDataGrid.Refresh(); 
} 
 
Query 2: “Bad Scrolling performance when we defined row height using QueryRowHeight” 
 
In your application QueryRowHeight is handled it will be raised every time new row comes to view to define row height. use the following code in QueryRowHeight handler to optimize scrolling performance. 
 
Code snippet: 
 
private void DataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e) 
{ 
    // Code to skip querying of a row if already queried 
    if (e.Height != sfDataGrid.RowHeight) 
        return; 
 
    if (e.RowIndex != 0) 
    { 
        //Calculates and sets height of the row based on its content  
        e.Height = (sender as SfDataGrid).GetRowHeight(e.RowIndex); 
        e.Handled = true; 
    } 
} 
 
Please refer the following UG link to improve scrolling performance in datagrid with QueryRowHeight. 
 
Please let us know if you need further assistance on this. 
 
Delete Bin and Obj folder in projects then file size will be around 1MB. 
 
Regards, 
Pradeep Kumar B. 



FE Frank Effenberger January 17, 2020 01:15 PM UTC

Thanks Pradeep, this fixed my problems so far :)

"Delete Bin and Obj folder in projects then file size will be around 1MB. "
I could have figured that out myself, sorry :p


FP Farjana Parveen Ayubb Syncfusion Team January 20, 2020 05:43 AM UTC

Hi Frank , 
 
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.
Live Chat Icon For mobile
Up arrow icon