0
votes

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>
1
Use the full functionality of WPF. Consider saving the data to a property and binding the DataGrid to it.user8478480
Do you see the blank row but not any column values?mm8
No I don't even see a blank row at all.nikhil
@nikhil: Are you seeing any rows at all then?mm8
When I say something like dr[0]=WinApp.Current.TryFindResource( "None" ).ToString(); dr[1]=WinApp.Current.TryFindResource( "None" ).ToString() etc and add this data to the DataTable I am able to see the data but then if l do like the below code inside a for loop, it dosen't show the data.nikhil

1 Answers

0
votes

Try this in else part:

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

I guess DataGrid's DataSource not notified as you add data after adding row in LotInformationDataTable.Rows