【问题标题】:Datagrid binding to results from databaseDatagrid 绑定到来自数据库的结果
【发布时间】:2012-03-12 14:06:12
【问题描述】:

我无法将 wpf 数据网格绑定到如下所示的数据:

id1   id2   result
1      10      X

1      11      Y

2      10      X

2      11      Z

其中每一行是一个具有 3 个成员的类实例。所以基本上我有这些类实例的集合,我想将它们绑定到具有可变列数的网格(取决于“id2”列有多少值)。

我想要得到的结果是这样的:

id1/id2 10  11
1       X   Y   
2       X   Z

在某些情况下只能有一列,在其他情况下是两列,所以我必须在后面的代码中创建列。

我已经为所有行和列值(Id1ItemsId2Items)创建了包装类,但我不确定将结果值存储在哪里以及如何创建绑定。理想情况下,我应该能够为第一列编写 "Id1Items.id1" 之类的绑定,为第二列编写 "Id2Items.id2" 之类的绑定,但是数据源集合应该是什么样子?

非常感谢。

【问题讨论】:

    标签: wpf binding datagrid


    【解决方案1】:

    有一个 column2,但如果你只有一个,就不要让它可见

     class MyDataContext
     {  
         public string column1Name get set  // bind this to the header1 
         public string column1Name get set  // bind this to the header2       
         public bool ColumnTwoVisible get set  // bind to this to make the column2 visible or not     
    
         public List<MyDataRow2> MyDataSet2
     }
    
    
     class MyDataRow2
     {
         public string id get set
         public string col1 get set
         public string col1 get set
     }
    

    【讨论】:

    • 我不想实现该属性以使第二列可见或不可见,我宁愿动态创建列,但这是一个小细节。我会尝试使用你的想法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2013-01-14
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多