How to pass the whole data object (for the grid line) to a converter in .NET MAUI DataGrid?
By default, .NET MAUI DataGrid passes the value associated with the property in a DisplayBinding.Converter. To pass the whole data object to the converter, you need to specify the BindingPath of the DisplayBinding as “.” (dot).
Refer to the below code, which illustrates setting BindingPath as “.” to pass the entire associated underlying data as a parameter to the Converter.
public class DataGridBehavior : Behavior<SfDataGrid>
{
protected override void OnAttachedTo(SfDataGrid dataGrid)
{
dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;
base.OnAttachedTo(dataGrid);
}
private void DataGrid_AutoGeneratingColumn(object? sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.MappingName == "Name")
{
e.Column.DisplayBinding = new Binding(".", BindingMode.Default, new ConvertCustomerName());
}
}
protected override void OnDetachingFrom(SfDataGrid dataGrid)
{
dataGrid.AutoGeneratingColumn -= DataGrid_AutoGeneratingColumn;
base.OnDetachingFrom(dataGrid);
}
}
The following code gives the implementation of the converter to get the data object.
public class ConvertCustomerName : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value != null)
{
return (value).ToString();
}
return null;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
On executing the above code, you will get the output as shown below:
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to pass the whole data object (for the grid line) to a converter.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!