We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to populate SfDataGrid through a data table in Xamarin.Android ?

Platform: Xamarin.Android |
Control: SfDataGrid |
Product Version: 13.4.0.53

To populate SfDataGrid through a DataTable, you must refer the System.Data assembly in your project.

Refer the below steps to add the System.Data assembly in your project.

  1. Right click on the reference of PCL project and click on Add Reference

Add Reference

  1. Choose Framework listed under Assemblies section in the left side panel and select System.Data and click ok.

System Data

 

Now, a DataTable collection can be directly bound to SfDataGrid.ItemsSource as shown in the below code example.

MainClass.cs

public class MainActivity : Activity
{
        private SfDataGrid sfGrid;
        private ViewModel_ DataTable viewModel;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            sfGrid = new SfDataGrid(this);
            viewModel = new ViewModel_ DataTable ();
            sfGrid.ItemsSource = viewModel.Records;
            sfGrid.AutoGenerateColumns = true;
            SetContentView(sfGrid);
        }
}

 

ViewModel_ DataTable.cs

public class ViewModel_DataTable : INotifyPropertyChanged
{
        public ViewModel_ DataTable ()
        {
            Records = GetRecords();
        }
        private DataTable records;
        public DataTable Records
        {
            get
            {
                return records;
            }
            set
            {
                records = value;
                RaisePropertyChanged("Records");
            }
        }
 
        private DataTable GetRecords()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Column1", typeof(int));
            dataTable.Columns.Add("Column2", typeof(string));
            dataTable.Columns.Add("Column3", typeof(int));
 
            dataTable.Rows.Add(new object[] { 1001, "Sean", 2001, "Jacobson", 3001, "Sean Jacobson", 4001, "Sean", 1001, "Jacobson" });
            dataTable.Rows.Add(new object[] { 1001, "Sean Jacobson", 2002, "Jacobson", 3002, "Sean Jacobson", 4002, "Sean", 1002, "Sean" });
            dataTable.Rows.Add(new object[] { 1003, "Jacobson", 2003, "Jacobson", 3003, "Sean Jacobson", 4003, "Sean", 1003, "Jacobson" });
            return dt;
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
}

 

Note:

View filtering is not supported by SfDataGrid while binding a DataTable collection.

 

Sample Link: How to populate SfDataGrid through a data table in Xamarin.Android?

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile