How to Bind DataTable to DataGrid in Xamarin | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
DataTable Binding Support in Xamarin DataGrid

How to Bind DataTable to DataGrid in Xamarin

As many of you know, .NET Standard 2.0 introduced support for DataSet and DataTable. With that, we got overwhelming demand for support to bind DataTable to the Xamarin.Forms DataGrid. We are happy to announce that this feature is available with our 2019 Volume 3 – Beta release.

In this blog, I am going to walk you through how to bind the DataTable class to the Xamarin.Forms DataGrid control.

Data binding

Let’s create a simple data table with Order details data. You won’t need any additional stuff for binding, just create the data table and assign it directly to DataGrid’s ItemsSource property. You can also bind the data table from the data set.

ViewModel creation

Create a view model class in the Xamarin project similar to the one in the following code example

public class ViewModel
{
    public ViewModel()
    {
        DataTableCollection = GetDataTable();
    }
    public DataTable DataTableCollection { get; set; }

    private DataTable GetDataTable()
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("Order ID", typeof(double));
        dataTable.Columns.Add("Customer Name", typeof(string));
        dataTable.Columns.Add("Customer ID", typeof(string));
        dataTable.Columns.Add("Country", typeof(string));
        dataTable.Rows.Add(1001, "Maria Anders", "ALFKI", "Germany");
        dataTable.Rows.Add(1002, "Ana Trujilo", "ANATR", "Mexico");
        dataTable.Rows.Add(1003, "Antonio Moreno","ENDGY", "Mexico");
        dataTable.Rows.Add(1004, "Thomas Hardy", "ANTON", "UK");
        dataTable.Rows.Add(1005, "Christina Berglund", "BERGS", "Sweden");
        dataTable.Rows.Add(1006, "Hanna Moos", "BLAUS", "Germany");
        dataTable.Rows.Add(1007, "Frederique Citeaux", "BLONP", "France");
        dataTable.Rows.Add(1008, "Martin Sommer", "BOLID", "Spain");
        dataTable.Rows.Add(1009, "Laurence Lebihan", "BONAP", "France");
        dataTable.Rows.Add(1010, "Kathryn", "BOTTM", "Canada");
        dataTable.Rows.Add(1011, "Tamer", "XDKLF", "UK");
        dataTable.Rows.Add(1012, "Martin", "QEUDJ", "US");
        dataTable.Rows.Add(1013, "Nancy", "ALOPS", "France");
        dataTable.Rows.Add(1014, "Janet", "KSDIO", "Canada");
        dataTable.Rows.Add(1015, "Dodsworth", "AWSDE", "Canada");
        dataTable.Rows.Add(1016, "Buchanan", "CDFKL", "Germany");
        dataTable.Rows.Add(1017, "Therasa", "WSCJD", "Canada");
        dataTable.Rows.Add(1018, "Margaret", "PLSKD", "UK");
        dataTable.Rows.Add(1019, "Anto", "CCDSE", "Sweden");
        dataTable.Rows.Add(1020, "Edward", "EWUJG", "Germany");
        dataTable.Rows.Add(1021, "Anne", "AWSDK", "US");
        dataTable.Rows.Add(1022, "Callahan", "ODKLF", "UK");
        dataTable.Rows.Add(1023, "Vinet", "OEDKL", "France"); 
        return dataTable;
    }
}

Binding ItemsSource in XAML

In the main page, add the necessary XML namespace to use SfDataGrid control, set the BindingContext of the page to the ViewModel class, and bind the ItemSource of SfDataGrid with the DataTableCollection.

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:DataGridDemo;assembly=DataGridDemo"  
             xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms" 
             x:Class="DataGridDemo.Sample">
<ContentPage.BindingContext>
        <local:ViewModel x:Name="ViewModel"/>
</ContentPage.BindingContext>

<ContentPage.Content>
    <syncfusion:SfDataGrid x:Name="dataGrid"
                           ItemsSource="{Binding DataTableCollection}">
    </syncfusion:SfDataGrid>
</ContentPage.Content>
</ContentPage>

DataTable binding in Xamrin Data Grid

DataTable binding in Xamarin Data Grid

CRUD operations

DataGrid automatically refreshes the UI whenever we add, delete or modify the rows of the underlying DataTable collection.

Data operations

DataGrid supports various data operations to represent the data in it. The following are supported by default:

  • Sorting (including multicolumn sorting)
  • Grouping (including multicolumn grouping)
  • Filtering
  • Summaries

Sorting

You can sort columns with the various built-in options such as multicolumn sorting and tri-state sorting. But custom sorting is not supported.

In the following screenshot, the data grid is sorted in ascending order with respect to the Customer Name column.Data grid is sorted in ascending order with respect to the Customer Name

Data Grid sorted ascending with respect to the Customer Name

Grouping

The data can be organized in a hierarchical structure based on matching field values. Multigrouping and custom grouping are also supported.Data grouped with respect to Country in Data Grid

Data grouped with respect to Country in Data Grid

Filtering

DataGrid’s built-in View.Filtering is not supported for the DataTable class. But, you can filter the records using DataTable’s DataView.RowFilter property.

In the following example, the DataView.RowFilter property is set as “[Country] = ‘Canada’”.Row filter applied to the Data Grid

Row filter applied to the Data Grid

Summaries

All summary types supported by DataGrid are supported for DataTable, i.e. group summary, caption summary, and table summary.Summaries displayed in the Data Grid

Summaries displayed in the Data Grid

Other features

The support for all other features, such as stacked headers, row drag and drop, unbound column, and unbound row, of DataGrid are available. You can refer to the user guide to learn more about the DataGrid’s feature sets.

Conclusion

I hope you enjoyed this quick walk-through of DataTable binding support in DataGrid. This support is available with our 2019 Volume 3 beta release, which you can download from here.

If you have any questions or require clarification about this feature, please let us know in the comments below. You can also contact us through our support forumDirect-Trac, or our feedback portal. We are happy to assist you!

Tags:

Share this post:

Comments (7)

[…] How to Bind DataTable to DataGrid in Xamarin (Neelakandan Kannan) […]

binding DataTable, but SwitchColumn binding not work how to binding ?

1. ‘True’ DataTable column string type not working

2. ‘true’ DataTable column bool type not working

,,, Class Binding is working

Hi 인식 남,

The switch column support to bind the `true` or `false` values of bool type properties. Binding is not supported for string type of properties. The reported issue with the “Switch column binding is not working when binding bool type of properties” has been fixed and the fix will be included in our upcoming 2019 Volume 3 Service Pack release.

A good tool to show data effeciently. I want to ask if this datagrid supports features like editing data, deleting, inserting and updating just like one in DevExpress DataGrid tool ?

Hi Farooq,
Thanks for your valuable feedback.
Our DataGrid supports all CRUD operations and all other essential features like sorting, grouping, summary rows and filtering. You can also check out our user guide and feature sets in the below link,
User Guide: https://help.syncfusion.com/xamarin/datagrid/data-binding
Feature tour: https://www.syncfusion.com/xamarin-ui-controls/xamarin-datagrid
Regards,
Suresh

hi,am having issue in the updating ui when i add delete insert or remove some row the ui is not updating

Hi Asfund,

Thanks for trying out Syncfusion’s datagrid and reaching out to us. I have checked the datagrid with a data table bound to it as the items source and tried all CRUD operations on it via button click and it is working fine. You can find the tested sample from this link https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGridSample_(4)-844355544.

Do check out the sample and let me know if my replication is any different from yours. Also I would like to know more details regarding your use scenario so that we could check and assist you better. Is grouping enabled in your grid?, does the issue have a specific replication procedure ?, which rows are not updating ( normal data row or summary rows) ?, how do you perform the crud operations ?, is the issue specific to any single platform ? etc. Awaiting to hear from you and resolve this for you.

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed