【问题标题】:WPF DataGrid not displaying values of DataTable in certain columnsWPF DataGrid 未在某些列中显示 DataTable 的值
【发布时间】:2015-09-18 20:39:14
【问题描述】:

我有一个程序对 CSV 文件进行一些处理,并生成一个 DataTable 作为输出。我将它绑定到 DataGrid,如下所示:

<DataGrid Name="GridTable" ItemsSource="{Binding OutputData}" />

似乎一切正常。生成 DataTable,更新 DataGrid。但莫名其妙地,这些列(Double 类型)似乎没有显示它们的数据。第一列(int 类型)工作正常:

当我仔细检查 DataTable 结构时,它就在那里,并且所有类型都正确。我没有例外:

数据绑定清楚地工作,因为它显示了列并且在第一列中具有正确的值。

以前有人遇到过这种情况吗?干杯

【问题讨论】:

    标签: c# wpf datagrid datatable


    【解决方案1】:

    你用了吗

    <DataGrid.Columns>
    <DataGridTextColumn Header="" Binding="{Binding }"/>
    </DataGrid.Columns> 
    

    它应该可以毫无问题地处理双打

    【讨论】:

      【解决方案2】:

      Marcin 上面的回答让我尝试单独绑定列,这让我来到这里:programmatically add column & rows to WPF Datagrid

      解决办法是这样的:

                  if (OutputData != null)
                  {
                      foreach (DataColumn col in OutputData.Columns)
                      {
                          GridTable.Columns.Add(
                            new DataGridTextColumn
                            {
                                Header = col.ColumnName,
                                Binding = new Binding(string.Format("[{0}]", col.ColumnName))
                            });
                      }
      
                      GridTable.DataContext = OutputData;
                  }
      

      【讨论】:

        【解决方案3】:

        我知道这已经很长时间了,但是如果您使用的是 db first 集成,那么可空值不会显示在 datagrid 和 excel 文件中。你基本上可以将你的 nullable 转换为 int

        【讨论】:

          猜你喜欢
          • 2019-03-14
          • 2011-01-03
          • 2017-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-08
          • 2012-03-14
          • 1970-01-01
          相关资源
          最近更新 更多