[UWP][C#] - Datagrid ItemsSource

Paula Morgan 276 Reputation points
2019-12-10T19:59:34.157+00:00

How can I bind a DataGrid's ItemsSource to list or array when I do not know the structure of the data? I know the column and row counts, but I need to create the object at run time that will bind to the Data grid.

Thanks for any suggestions.

Paula

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Elmar 76 Reputation points
    2019-12-12T19:34:27.527+00:00

    Set on datagrid this: AutoGenerateColumns = true. Whit this setting the datagrid creates the columns automatically in base of the object.

    For sorting set this CanUserSortColumns = true.

    In backend sort the list:

            private List orginalSource = new List();  
            private List sortedItems = new List();  
            private Dictionary itemProperties = new Dictionary();  
      
            public List SortedItems  
            {  
                get => sortedItems;   
                set  
                {  
                    sortedItems = value;  
                    OnPropertyChanged();  
                }  
            }  
      
            private void InitializeList()  
            {  
                //Get list from the database  
                SortedItems = orginalSource = GetFromDb();  
      
                //initialize data structure to get the properties  
                itemProperties = new Dictionary();  
      
                var firstItem = sortedItems?.First();  
                if (firstItem != null)  
                {  
                    foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(firstItem))  
                    {  
                        itemProperties[propertyDescriptor.Name] = propertyDescriptor;  
                    }  
                }  
            }  
      
            private async void DataGrid_Sorting(object sender, DataGridColumnEventArgs e)  
            {  
                if (e.Column.SortDirection != DataGridSortDirection.Ascending)  
                {  
                    e.Column.SortDirection = DataGridSortDirection.Ascending;  
                    SortedItems = orginalSource.OrderBy(x => GetDynamicValue(x, e.Column.Header.ToString())).ToList();  
                }  
                else  
                {  
                    e.Column.SortDirection = DataGridSortDirection.Descending;  
                    SortedItems = orginalSource.OrderByDescending(x => GetDynamicValue(x, e.Column.Header.ToString())).ToList();  
                }  
      
                //wait a little bit, that the datagrid can recreate all  
                await Task.Delay(10);  
      
                DataGrid dg = sender as DataGrid;  
      
                //reset sort direction of the column  
                foreach (var column in dg.Columns)  
                {  
                    if(column.Header == e.Column.Header)  
                    {  
                        column.SortDirection = e.Column.SortDirection;  
                    }  
                }  
            }  
      
            private object GetDynamicValue(dynamic item, string propertyName)  
            {  
                if (itemProperties.ContainsKey(propertyName))  
                    return itemProperties[propertyName].GetValue(item);  
                else  
                    return null;  
            }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful