简体   繁体   中英

How to bind Wpf datagrid to dynamically created column name,value pairs?

I have multiple sets of Lists, which contain data regarding the column header and the value for that column.For any particular set the column headers remain the same.But they are different for different sets.Each list in a set will represent a row in the data grid.At a particular time one set's data will be displayed in the grid.Hence, the columns headers will be same for the all the list within that set.And the values in the list will be shown in rows. Suppose we have two lists:

List<Data> l1 = new List<Data>(){new Data(){Header="A",Value="A1"},new Data(){Header="B",Value="B1"}};
List<Data> l2 = new List<Data>(){new Data(){Header="A",Value="A2"},new Data(){Header="B",Value="B2"}};

The data should be displayed in the grid as:

  A       B
  A1      B1
  A2      B2

So, how do I achieve this , as for creating bindings I have to create public properties.And I can not hard code these properties, as they are dynamic.

I solved this problem by using the ASh's suggestion of using DataTable.Below is the code that I used for converting my data to DataTable.

public static DataTable GenerateDataGridFromMetaData(List<ObservableCollection<DocumentMetaData>> dataList)
{
    try
    {
        if (dataList.Count > 0)
        {
                    DataTable dataTable = new DataTable();
                    List<int> skipColumn = new List<int>();
                    for (int index = 0; index < dataList.Count; index++)
                    {
                        ObservableCollection<DocumentMetaData> metaDataList = dataList[index];
                        List<string> values = new List<string>();
                        for (int metaDataIndex = 0; metaDataIndex < metaDataList.Count; metaDataIndex++)
                        {
                            DocumentMetaData docMetaData = metaDataList[metaDataIndex];
                            if (index == 0)
                            {
                                if (!dataTable.Columns.Contains(docMetaData.Title))
                                {
                                    dataTable.Columns.Add(docMetaData.Title);
                                    values.Add(docMetaData.Value); 
                                }
                                else
                                {
                                    skipColumn.Add(metaDataIndex);
                                }
                            }
                            else
                            {
                                if (!skipColumn.Contains(metaDataIndex))
                                {
                                    values.Add(docMetaData.Value); 
                                }
                            }
                        }
                        dataTable.Rows.Add(values);
                    }
                    return dataTable;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
            }    
        }

And this is the code that I used to bind data table to grid.

DataTable table = GridViewConverter.GenerateDataGridFromMetaData(dataList);
DataGrid.ItemsSource = table.DefaultView;

Please, ensure that AutoGenerateColumns property is set to true, else grid will not display data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM