【发布时间】: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 循环中喜欢下面的代码,它会增加'不显示数据。