简体   繁体   English

如何使用c#在DataGrid(WPF)中显示列表列表

[英]How to display a list of list in DataGrid( WPF) using c#

I have a List of headers(columns) and then rows of data and want to show it in DataGrid with two way bindings我有一个标题列表(列),然后是数据行,并希望通过两种方式绑定在 DataGrid 中显示它

List<string> headers = new List<string> { "FirstName", "LastName", "Age" };
List<string> row1 = new List<string> { "John", "Doe", "19" };
List<string> row2 = new List<string> { "Jane", "Doe", "21" };
List<string> row3 = new List<string> { "Suzie", "Q", "52" };
List<string> row4 = new List<string> { "No", "Body", "48" };

List<List<string>> tableValues =
 new List<List<string>> { row1, row2, row3, row4 };

The editor does not let me show List of List since it has multiple <编辑器不允许我显示列表列表,因为它有多个 <

I appreciate any help.我很感激任何帮助。

given that number of headers may vary, I suggest to tranform data in the format convenient for two-way data binding and use DataTable:鉴于标题的数量可能会有所不同,我建议以方便双向数据绑定的格式转换数据并使用 DataTable:

var dt = new DataTable();

// create columns and headers
int columnCount = headers.Count;
for (int i = 0; i < columnCount; i++)
    dt.Columns.Add(headers[i]);

// copy rows data
for (int i = 0; i < tableValues.Count; i++)
    dt.Rows.Add(tableValues[i].Take(columnCount).ToArray());

// display in a DataGrid
dataGrid.ItemsSource = dt.DefaultView;

Firstly create a class to hold your person information because how you are initializing your lists isn't good.首先创建一个类来保存您的个人信息,因为您初始化列表的方式并不好。

Public class Person
{
    public string Firstname {get; set;}
    public string Surname {get; set;}
    public int Age {get; set;}
}

Then you can create multiple people and store them in a list of type Person...eg然后你可以创建多个人并将他们存储在一个 Person 类型的列表中......例如

//other code
List<Person> People = new List<Person>();
People.Add(new Person() { Firstname = "John", Surname = "Doe", Age = 19 });
//etc

Then in XAML all you have to do is point your Data grid at the People list and it should be able to bind to the various properties on each person or auto generate columns.然后在 XAML 中,您所要做的就是将数据网格指向人员列表,它应该能够绑定到每个人的各种属性或自动生成列。

<DataGrid CanUserSortColumns="True" 
          CanUserAddRows="False"
          CanUserDeleteRows="False" 
          AutoGenerateColumns="False"
          ItemsSource="{Binding Path=People}"
          SelectionMode="Single">

    <DataGrid.Columns>
         <DataGridTextColumn     MinWidth="100"
                                         Width="Auto"
                                         IsReadOnly="False"
                                         Header="Firstname">
                                    <DataGridTextColumn.Binding>
                                        <Binding Path="Firstname"/>
                                    </DataGridTextColumn.Binding>
                  </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

untested XAML but should give you a starting point未经测试的 XAML 但应该给你一个起点

There are plenty of guides online that will be able to help with this instead of writing your own question on Stack Overflow.网上有很多指南可以帮助解决这个问题,而不是在 Stack Overflow 上写你自己的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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