【问题标题】:Datatable with single row doesn't display in DataGrid单行数据表不显示在 DataGrid 中
【发布时间】:2019-02-05 01:36:57
【问题描述】:

我有以下代码,其中我有一个绑定到 DataGrid 的 DataTable。如果 SelectedLot 不为空,则将数据添加到 DataTable 并且数据正在正确显示。但是,如果 SelectedLot 为空,我想显示一个空白行,其中包含“None”字符串。出于某种原因,当我调试时它清楚地说它有一行,但它没有显示在 DataGrid 中。请帮忙。

数据表代码

public void PopulateLotDataTable(PlateSetupViewModelEx2 plateViewModel)
{
    try
    {
        if (LotInformationDataTable == null)
        {
            LotInformationDataTable = new DataTable();
        }
        LotInformationDataTable.Columns.Add(WinApp.Current.TryFindResource("LotType").ToString());
        LotInformationDataTable.Columns.Add(WinApp.Current.TryFindResource("CatalogNumber").ToString());
        LotInformationDataTable.Columns.Add(WinApp.Current.TryFindResource("SupermixColumnHeader").ToString());
        LotInformationDataTable.Columns.Add(WinApp.Current.TryFindResource("LotNumber").ToString(), typeof(string));
        DataRow dr = null;
        var SelectedLot = plateViewModel.PlateSetupToUse.GetAllWellSamples().Values.FirstOrDefault(x => x.SelectedLots != null);
        if (SelectedLot != null)
        {
            foreach (var item in SelectedLot?.SelectedLots)
            {
                foreach (var subitem in item?.KitLots)
                {
                    dr = LotInformationDataTable.NewRow();
                    dr[0] = EnumExtensions.ToDescriptionString(item.KitLotType);
                    dr[1] = subitem.CatalogNumber;
                    dr[2] = subitem.Supermix;
                    dr[3] = subitem.LotNumber;
                    LotInformationDataTable.Rows.Add(dr);
                }
             }
         }
         else
         {
             dr = LotInformationDataTable.NewRow();
             LotInformationDataTable.Rows.Add(dr);
             for (int i = 0; i < LotInformationDataTable.Columns.Count; i++)
             {
                 LotInformationDataTable.Rows[0][i] = WinApp.Current.TryFindResource("None").ToString(); //This is not working I guess.
             }
             Debug.Write(LotInformationDataTable.Rows.Count);
        }
    }
    catch(Exception ex)
    {
        SystemDebugLogLogger.LogException(ex);
    }
}

Xaml

 <DataGrid Grid.Row="2" IsReadOnly="True" ColumnWidth="*" CanUserAddRows="False" Margin="{StaticResource AllControlsMargin}" ItemsSource="{Binding RunViewModel.LotInformationDataTable}" AutoGenerateColumns="True" IsHitTestVisible="False">
 </DataGrid>

【问题讨论】:

  • 使用WPF 的全部功能。考虑将数据保存到属性并将DataGrid 绑定到它。
  • 您看到空白行但没有看到任何列值吗?
  • 不,我什至没有看到空白行。
  • @nikhil:那你有没有看到任何行?
  • 当我说类似 dr[0]=WinApp.Current.TryFindResource("None").ToString(); dr[1]=WinApp.Current.TryFindResource("None").ToString() 等并将这些数据添加到 DataTable 我能够看到数据但是如果我在 for 循环中喜欢下面的代码,它会增加'不显示数据。

标签: wpf datatable datagrid


【解决方案1】:

在else 部分试试这个:

dr = LotInformationDataTable.NewRow();

// First add data in empty row
for ( int i = 0; i < LotInformationDataTable.Columns.Count; i++ )
{
    dr[i] = WinApp.Current.TryFindResource( "None" ).ToString();
}

// Notify UI by adding row in collection.
LotInformationDataTable.Rows.Add( dr );

Debug.Write( LotInformationDataTable.Rows.Count );

我猜DataGrid 的DataSource 在LotInformationDataTable.Rows 中添加行后添加数据时未收到通知

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    相关资源
    最近更新 更多