I have the following code where I have a DataTable which I am binding to DataGrid. If the SelectedLot is not null the Data gets added to the DataTable and the data is being displayed properly. But if the SelectedLot is null I want to display a blank row with "None" string in it. For some reason when I debug it clearly says it has got one row in it, but it doesn't get displayed in the DataGrid. Please help.
Code for DataTable
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
. Consider saving the data to a property and binding theDataGrid
to it. – user8478480