简体   繁体   English

将DataGrid绑定到列表<object[]>

[英]databinding DataGrid to a List<object[]>

I have a List<object[]> MyCollection which is a result of a SELECT SQL query. 我有一个List<object[]> MyCollection ,它是SELECT SQL查询的结果。
Each object[] represents a row in the query result, and the length of the array will vary for different queries. 每个object[]代表查询结果中的一行,并且数组的长度将因不同的查询而异。
I have the name of the column headers in a separate List<string> MyHeaders . 我在单独的List<string> MyHeaders中具有列标题的名称。

I want to databind MyCollection to a DataGrid with the header of the columns from MyHeaders , and autogenerate the columns. 我想数据绑定MyCollection一个DataGrid与列从头部MyHeaders ,并自动生成列。

The reason I want to use AutoGenerateColumns is because I want the Datagrid to reconize the DataTypes of each object, and use the appropriate Column Templates for each DataType. 我想使用AutoGenerateColumns的原因是因为我希望Datagrid重新协调每个对象的DataType,并为每个DataType使用适当的列模板。

Thanks! 谢谢!

If you're stuck and need to get on: use a dataset/ datatable. 如果您遇到困难并需要继续下去,请使用数据集/数据表。 Ancient but they still work fine! 古老,但它们仍然可以正常工作! If you want to know if it can be done, i don't know. 如果您想知道是否可以做到,我不知道。 Most examples of binding to an IList I've seen manually loop and create columns. 我看到的大多数绑定到IList的示例都是手动循环并创建列的。

Regards GJ 关于GJ

Don't use a list for the headers, use a dictionary and use the property names as the keys and put the description as the values. 标题不要使用列表,请使用字典,并使用属性名称作为键,并在描述中添加值。 Do a normal blah.DataSource = List and DataBind(). 做一个普通的blah.DataSource = List和DataBind()。 In your itemdatabound event replace the headers with your description by searching the dictionary with something like this: 在您的itemdatabound事件中,通过像这样搜索字典来将标题替换为描述:

foreach col in grid.Columns { col.Name = dic[col.Name].Value; }

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

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