我有下面的代码,我有一个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>
在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
添加一行之后,添加数据