开发者

How to bind an IEnumerable collection to a Datagrid (items of variable contents)

While working with an excel file (.xlsx), the data I get f开发者_JAVA技巧rom it is an IEnumerable(of Dictionary). When I bind this to my datagrid, I get the problem that I only see 2 columns. I've been tracing this problem and discovered that in the first row in the excel, there are only 2 values (cell C and D). I think the binding to datagrid looks at this first row to create its headers. This results in my datagrid only showing columns C and D.

The second row for example can contain info in column A, B, E and G, but as only columns C and D are shown, this row won't show data in the datagrid.

Is there a way to bind this variable length data to the datagrid?


A DataGrid by definition displays a regular grid of information. As you surmised, if auto generate headers is on, it looks at the first record only as it should (what else could it do... with paged data etc?).

If you want to display it in a DataGrid the data needs to be uniform. That means that when you get your data from your dictionary you should probably populate a class that has A, B, C, D, E & G and just leave the unused entries blank.

(If you define the columns and bindings in advance manually you might be able to bind the dictionary directly, but I have never tried sparce/irregular data on a DataGrid... give it a try).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜