Category / Section
How to bind dynamic data object with SfDataGrid?
1 min read
SfDataGrid provides support to bind dynamic data object by setting collection of dynamic or ExpandoObject to SfDataGrid.ItemsSource property.
MainActivity.cs
SfDataGrid dataGrid; dataGrid = new SfDataGrid(this); employeeCollection = new EmployeeCollection(); dataGrid.ItemsSource = employeeCollection.EmployeeDetails;
EmployeeCollection.cs
public class EmployeeCollection { Random random = new Random(); public EmployeeCollection() { EmployeeDetails = GetEmployeesDetails_Dynamic(200); } #region ItemsSource private ObservableCollection<dynamic> _employeeDetails; public ObservableCollection<dynamic> EmployeeDetails { get { return _employeeDetails; } set { _employeeDetails = value; RaisePropertyChanged("EmployeeDetails"); } } #endregion // Dynamic DataSource public ObservableCollection<dynamic> GetEmployeesDetails_Dynamic(int count) { var employees = new ObservableCollection<dynamic>(); for (int i = 1; i < count; i++) { employees.Add(GetDynamicEmployee(i)); } return employees; } // Dynamic Property public dynamic GetDynamicEmployee(int i) { dynamic employee = new ExpandoObject(); employee.EmployeeName = employeeName[random.Next(14)]; employee.EmployeeID = i; employee.ContactID = i + 100; return employee; } string[] employeeName = new string[] { "Sean Jacobson", "Phyllis Allen", "Marvin Allen", "Michael Allen", "Cecil Allison", "Oscar Alpuerto", "Sandra Altamirano", "Selena Alvarad", "Emilio Alvaro", "Maxwell Amland", "Mae Anderson", "Ramona Antrim", "Sabria Appelbaum", "Hannah Arakawa", };
Screen shot
SampleLink: How to bind dynamic data object with SfDataGrid?