数据表与单排DataGrid中不显示

问题描述 投票:0回答:1

我有下面的代码,我有一个DataTable,其中我绑定到数据网格。如果SelectedLot不为null,数据被添加到数据表,并正确显示数据。但是,如果SelectedLot是空我想显示一个空行,在它“无”字符串。因为当我调试它的一些原因,明确表示,它已经得到了一个排它,但它不会显示在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 datatable datagrid
1个回答
0
投票

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 );

我想没有通知DataGridDataSource如您在LotInformationDataTable.Rows添加一行之后,添加数据

© www.soinside.com 2019 - 2024. All rights reserved.