How to create hyperlink column in .NET MAUI DataGrid?
The .NET MAUI DataGrid (SfDataGrid) does not natively support hyperlink columns, but you can implement them using a DataGridTemplateColumn with a custom label.
XAML
Define your DataGrid with the required customizations. Use a DataGridTemplateColumn
to load a customized label for your hyperlink column.
<syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding ControlInfoCollection}"
ColumnWidthMode="Auto">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="Control"/>
<syncfusion:DataGridTextColumn MappingName="Platform"/>
<syncfusion:DataGridTemplateColumn MappingName="UGLink">
<syncfusion:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:HyperlinkLabel Text="User Guide documentation"
Url="{Binding UGLink}"
HorizontalOptions="Center"/>
</DataTemplate>
</syncfusion:DataGridTemplateColumn.CellTemplate>
</syncfusion:DataGridTemplateColumn>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
C#
Define a custom label named HyperlinkLabel
that is inherited from Label
. This customized control will represent the clickable hyperlink within the template column.
HyperlinkLabel.cs
public class HyperlinkLabel : Label
{
public static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(HyperlinkLabel), null);
public string Url
{
get { return (string)GetValue(UrlProperty); }
set { SetValue(UrlProperty, value); }
}
public HyperlinkLabel()
{
TextDecorations = TextDecorations.Underline;
TextColor = Colors.Blue;
GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(async () => await Launcher.OpenAsync(Url))
});
}
}
The following screenshot shows the Hyperlink Column in SfDataGrid.
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to create hyperlink columns in MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out 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. We are always happy to assist you!