Articles in this section
Category / Section

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

2 mins read

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?

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied