How to bind DataTable collection to MAUI DataGrid (SfDataGrid)?
The .NET MAUI DataGrid(SfDataGrid) allows binding DataTable as ItemsSource. DataGrid automatically refreshes the UI when binding DataTable as ItemsSource when rows are added, deleted or cleared.
C#
Populate items to the DataTable.
public class ViewModel { public DataTable EmployeeCollection { get; set; } public ViewModel() { this.CreateDataTable(); } private void CreateDataTable () { EmployeeCollection = new DataTable(); EmployeeCollection.Columns.Add("ID", typeof(string)); EmployeeCollection.Columns.Add("Company", typeof(string)); EmployeeCollection.Columns.Add("Name", typeof(string)); EmployeeCollection.Columns.Add("City", typeof(string)); EmployeeCollection.Rows.Add("ALFKI", "Alferds Futterkiste", "Maria Anders", "Berlin"); EmployeeCollection.Rows.Add("ANATR", "Ana Trujilo Emparedados y Hela", "Ana Trujilo", "Mexico D.F."); EmployeeCollection.Rows.Add("ANTON", "Antonio Moreno Taqueria", "Antonio Moreno", "Mexico D.F."); EmployeeCollection.Rows.Add("AROUT", "Around the Horn", "Thomas Hardy", "London"); EmployeeCollection.Rows.Add("BERGS", "Berglunds Snabbkop", "Christina Berglund", "Lulea"); EmployeeCollection.Rows.Add("BLAUS", "Blauer see Delikatessen", "Hanna Moss", "Mannheim"); EmployeeCollection.Rows.Add("BLONP", "Blondel Pere et Fils", "Erederique Citeaux", "Strasbourg"); EmployeeCollection.Rows.Add("BOLID", "Bolids Comidas Preparadas", "Martin Sommer", "Madrid"); EmployeeCollection.Rows.Add("BONP", "Bon App", "Laurence Lebihan", "Marseille"); EmployeeCollection.Rows.Add("BOTTM", "Bottom-Dollar Markets", "Elizabeth Lincoln", "Tsawassen"); EmployeeCollection.Rows.Add("BSBEV", "B's Beverages", "Victoria Ashworth", "London"); EmployeeCollection.Rows.Add("CACTU", "Cactus Comidas para llevar", "Patricio Simpson", "Bueno Aires"); EmployeeCollection.Rows.Add("CENTC", "Centro Comercial Moctezuma", "Francisco Chang", "Mexico D.F."); EmployeeCollection.Rows.Add("CHOPS", "Chop-Suey Chinese", "Yang Wang", "Bern"); EmployeeCollection.Rows.Add("COMMI", "Comercio Minerio", "Pedro Afonso", "Sao Paulo"); EmployeeCollection.Rows.Add("CONSH", "Consolidated Holdings", "Elizabeth Brown", "London"); EmployeeCollection.Rows.Add("DRACD", "Drachenblut Entier", "Sven Ottlieb", "Aachen"); EmployeeCollection.Rows.Add("DUMON", "Dumonde Entier", "Janine Labrune", "Nantes"); EmployeeCollection.Rows.Add("EASTC", "Eastern Connection", "Ann Devon", "London"); EmployeeCollection.Rows.Add("ERNSH", "Ernst Handel", "Roland Mendel", "Graz"); } }
XAML
Bind the ViewModel collection to the `ItemsSource` property.
<syncfusion:SfDataGrid x:Name="dataGrid" ItemsSource="{Binding EmployeeCollection}" DefaultColumnWidth="130" GridLinesVisibility="Both" HeaderGridLinesVisibility="Both"> </syncfusion:SfDataGrid>
Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid(SfDataGrid) with code examples.
Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid(SfDataGrid).
Conclusion
I hope you enjoyed learning about how to bind DataTable collection to MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid’s feature tour page to know 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, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!