Refreshing a row height programmatically
Hello!
I am looking for a way to refresh the height of the row after the content of a cell changes in the SfDataGrid.
In Xamarin, I used the SfDataGrid.InvalidateRowHeight() method to do this. In Maui, according to your documentation, It looks like I have to capture the DataGrid_QueryRowHeight event and use the GetIntrinsicRowHeight method :
private void DataGrid_QueryRowHeight(object sender, DataGridQueryRowHeightEventArgs e)
{
if (e.RowIndex != 0)
{
//Calculates and sets the height of the row based on its content.
e.Height = e.GetIntrinsicRowHeight(e.RowIndex);
e.Handled = true;
}
}But is there a way to trigger this event programmatically after the content of a grid cell changes, in order to refresh the row height?
Thank you for your assistance!
Alfredo Diaz
Hi Alfredo,
As of now, the InvalidateRowHeight method is an internal function of the datagrid. You can utilize reflection to access this method and invoke QueryRowHeight programmatically. We've provided a code snippet for your reference. By employing a button click to modify a cell value to a longer string and invoking this method, you can trigger the QueryRowHeight at runtime.
Code snippet:
|
private void Button_Clicked(object sender, EventArgs e) { viewModel.Employees[0].Name = "A very long string to test !!!!!!!!"; typeof(SfDataGridHelpers).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault(x => x.Name == "InvalidateRowHeight")!.Invoke(null, [dataGrid, 1, true]); } |
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Regards,
Nirmalkumar
Thank you Nirmalkumar.
Can you post what the parameters are for the InvalidateRowHeight() method?
Thanks,
Alfredo Diaz
Hi Alfredo,
The parameters for InvalidateRowHeight() are int rowIndex, with bool canInvalidate defaulting to false.
Code snippet:
|
private void Button_Clicked(object sender, EventArgs e) {
viewModel.Employees[0].Name = "A very long string to test !!!!!!!!"; typeof(SfDataGridHelpers).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault(x => x.Name == "InvalidateRowHeight")!.Invoke(null, [dataGrid, 1, true]); } |
Regards,
Nirmalkumar
Hello,
I am attempting to use this method described above to trigger a call to QueryRowHeight programmatically, but the following call to GetMethods is returning null for the methodInfo:
MethodInfo methodInfo = typeof(SfDataGridHelpers).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault(x => x.Name == "InvalidateRowHeight");
It looks like there is no such method in the SfDataGridHelpers class. Can you confirm that this is still the correct way to trigger a call to QueryRowHeight programmatically?
Thank you for your assistance,
Alfredo Diaz
Hi Alfredo,
Previously, we had defined the InvalidateRowHeight method as private, which led us to suggest using reflection. However, we have now made this method public, so you can achieve this with the code below.
Code snippet:
|
private void sfGrid_QueryRowHeight(object sender,Syncfusion.Maui.DataGrid.DataGridQueryRowHeightEventArgs e) { if (e.RowIndex > 0) { e.Height = e.GetIntrinsicRowHeight(e.RowIndex); e.Handled = true; (sender as SfDataGrid).InvalidateRowHeight(e.RowIndex, true); } } |
We had shared the simple sample for your reference.
Regards,
Sethupathy D.
Attachment: SfDataGridSample_32face93.zip
That worked. Thank you!
Hi Alfredo,
We are glad to know that the provided solution worked at your end. Please let us know if you have any further queries on this. We are happy to help.
Regards,
Sethupathy D.
- 7 Replies
- 3 Participants
- Marked answer
-
AL Alfredo
- Mar 10, 2024 03:13 PM UTC
- Jan 21, 2025 07:11 AM UTC