How to show no records found message in .NET MAUI DataGrid (SfDataGrid) with empty data?
In this article, we will demonstrate how to display “No Records found” message when the data source is empty in .NET MAUI DataGrid.
xaml
<ContentPage.BindingContext>
<local:EmployeeViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
<StackLayout>
<HorizontalStackLayout Margin="10">
<Button Text="clear ItemSource" WidthRequest="200"
Clicked="Button_Clicked" />
<Button Text="Add ItemSource" WidthRequest="200"
Clicked="Button_Clicked_1" />
</HorizontalStackLayout>
<Label Margin="50,200,50,100" x:Name="label"
FontSize="Large"
Text="No Records are Found :("
HorizontalOptions="Center"
VerticalOptions="End" />
<syncfusion:SfDataGrid x:Name="sfGrid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="10"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ColumnWidthMode="Auto"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Name" HeaderText="Employee Name" />
<syncfusion:DataGridTextColumn MappingName="Title" HeaderText="Designation" />
<syncfusion:DataGridDateColumn MappingName="HireDate" HeaderText="Hire Date"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</StackLayout>
C#
The following code toggles the visibility of a Syncfusion® DataGrid and a label based on whether the grid’s data source is null or empty, with buttons to clear or repopulate the DataGrid.
public MainPage()
{
InitializeComponent();
sfGrid.ItemsSourceChanged += SfGrid_ItemsSourceChanged;
}
private void SfGrid_ItemsSourceChanged(object? sender, Syncfusion.Maui.DataGrid.DataGridItemsSourceChangedEventArgs e)
{
if (e.NewView == null || (e.NewView is ObservableCollection<Employee> employees && employees.Count == 0))
{
sfGrid.IsVisible = false;
label.IsVisible = true;
}
else
{
// Otherwise, show the grid and hide the label
sfGrid.IsVisible = true;
label.IsVisible = false;
}
}
private void Button_Clicked(object sender, EventArgs e)
{
sfGrid.ItemsSource = null;
}
private void Button_Clicked_1(object sender, EventArgs e)
{
sfGrid.ItemsSource = viewModel.Employees;
}
Output
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to show a “No records found” message in .NET MAUI DataGrid (SfDataGrid) with empty data.
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 components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
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!