DataGrid data not populating as expected

Viewed 40

I have a DataGrid that is populated from data of a simple, 1 row datatable. However, the datatable is not populating each of the columns as expected. Some cells are correct, while others are not. I am using a button click event to populate the datatable row.

Here is the XML code that defines the DataGrid:

 <DataGrid x:Name="DG_TimedTest" HorizontalAlignment="Left" Height="86" Margin="0,118,0,0" VerticalAlignment="Top" Width="1296" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ColumnHeaderHeight="60" Background="#FF232E3C" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" SelectionMode="Single" SelectionUnit="Cell" CanUserResizeRows="False" HorizontalGridLinesBrush="#FF688CAF" VerticalGridLinesBrush="#FF688CAF" HeadersVisibility="Column" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled" Foreground="#FFC8E5FF" Grid.ColumnSpan="2" RowStyle="{DynamicResource RowStyle1}" ColumnHeaderStyle="{DynamicResource DGCHeaderStyle}" CanUserAddRows="False" EnableRowVirtualization="False">
                                    <DataGrid.BindingGroup>
                                        <BindingGroup/>
                                    </DataGrid.BindingGroup>
                                </DataGrid>

Here is the XML code that defines the Header style:

<Style x:Key="DGCHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Height" Value="48"/>
<Setter Property="Background" Value="#FF232E3C" />
<Setter Property="Foreground" Value="#FFC8E5FF"/>
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="BorderBrush" Value="#FF688CAF"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>

Here is the XML code defining the Row style:

<Style x:Key="RowStyle1" TargetType= "{x:Type DataGridRow}">
<Setter Property="Background" Value="#FF232E3C"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>

Here is the code behind that defines the DataTable:

Public TestTimedTable As DataTable = New DataTable()


    'Define the columns for the datatable
    Dim MotorCCW3 As New DataColumn("Rotation" & vbCrLf & "  (CCW)", GetType(Boolean))
    Dim MotorCW3 As New DataColumn("Rotation" & vbCrLf & "   (CW)", GetType(Boolean))
    Dim AccelRate3 As New DataColumn("Accel Rate" & vbCrLf & "  (RPM/S)", GetType(Integer))
    Dim DecelRate3 As New DataColumn("Decel Rate" & vbCrLf & "  (RPM/S)", GetType(Integer))
    Dim Speed3 As New DataColumn("Speed" & vbCrLf & "(RPM)", GetType(Integer))
    Dim LowFlowPump3 As New DataColumn(" Pump" & vbCrLf & "(Lo-Flo)", GetType(Boolean))
    Dim HiFlowPump3 As New DataColumn(" Pump" & vbCrLf & "(Hi-Flo)", GetType(Boolean))
    Dim Flow3 As New DataColumn("Flow" & vbCrLf & "(CCM)", GetType(Integer))
    Dim FlowDev3 As New DataColumn("Flow Deviation" & vbCrLf & "          (%)", GetType(Integer))
    Dim TankTemp3 As New DataColumn("Tank Fluid" & vbCrLf & "     (°F)", GetType(Integer))
    Dim ExitFluidLowLim3 As New DataColumn("Exit Fluid LO-LIM" & vbCrLf & "            (°F)", GetType(Integer))
    Dim PartTempLim3 As New DataColumn("Part HI-LIM" & vbCrLf & "       (°F)", GetType(Integer))
    Dim TorqueLim3 As New DataColumn("Torque HI-LIM" & vbCrLf & "       (Nm)", GetType(Single))
    Dim Divert3 As New DataColumn("Divert Oil", GetType(Boolean))
    Dim DivertTimeOn3 As New DataColumn(" Diverter On" & vbCrLf & "        (S)", GetType(Integer))
    Dim DivertTimeOff3 As New DataColumn(" Diverter Off" & vbCrLf & "        (S)", GetType(Integer))
    
    
    'Add the columns to the datatable
    TestTimedTable.Columns.Add(MotorCCW3)
    TestTimedTable.Columns.Add(MotorCW3)
    TestTimedTable.Columns.Add(AccelRate3)
    TestTimedTable.Columns.Add(DecelRate3)
    TestTimedTable.Columns.Add(Speed3)
    TestTimedTable.Columns.Add(LowFlowPump3)
    TestTimedTable.Columns.Add(HiFlowPump3)
    TestTimedTable.Columns.Add(Flow3)
    TestTimedTable.Columns.Add(FlowDev3)
    TestTimedTable.Columns.Add(TankTemp3)
    TestTimedTable.Columns.Add(ExitFluidLowLim3)
    TestTimedTable.Columns.Add(PartTempLim3)
    TestTimedTable.Columns.Add(TorqueLim3)
    TestTimedTable.Columns.Add(Divert3)
    TestTimedTable.Columns.Add(DivertTimeOn3)
    TestTimedTable.Columns.Add(DivertTimeOff3)
    
    'Set the ItemsSource for the DataGrid as the DataTable
    DG_TimedTest.ItemsSource = TestTimedTable.DefaultView

Via a button click event, populate a row with some data and add the row to the DataTable:

'Button click event, populate the datatable
    Private Sub MyButton2_Click(sender As Object, e As RoutedEventArgs) Handles MyButton2.Click
    Dim i As Integer = 35
    Dim ii As Integer = 45
    Dim s As Single = 3.14
    Dim b As Boolean = True
    Dim bb As Boolean = False
    TestTimedTable.TableName = "Test_Timed_Table"

    TestTimedTable.Rows.Add(b, b, i, ii, i, b, b, i, ii, i, ii, i, s, b, i, ii)
    End Sub

Here is what the DataTable visualizer looks like, appears fine. enter image description here

Here is the front end DataGrid (First 12 cells) enter image description here

Here is an image of the remaining cells of the DataGrid: enter image description here

The Datagrid is not being populated with the DataTable data all the way through. Some cells are not populating correctly, starting at the "Pump (Lo-Flo)" column. Any ideas what I require to do to fix the anomalies?

Thank you

1 Answers

Removing the space before the header column names has fixed the problem. Apparently putting a space before the start of the column name will cause an anomaly.

Here is the updated code defining the columns. Spaces removed before the start of the column names:

Dim MotorCCW3 As New DataColumn("Rotation" & vbCrLf & "  (CCW)", GetType(Boolean))
    Dim MotorCW3 As New DataColumn("Rotation" & vbCrLf & "   (CW)", GetType(Boolean))
    Dim AccelRate3 As New DataColumn("Accel Rate" & vbCrLf & "  (RPM/S)", GetType(Integer))
    Dim DecelRate3 As New DataColumn("Decel Rate" & vbCrLf & "  (RPM/S)", GetType(Integer))
    Dim Speed3 As New DataColumn("Speed" & vbCrLf & "(RPM)", GetType(Integer))
    Dim LowFlowPump3 As New DataColumn("Pump" & vbCrLf & "(Lo-Flo)", GetType(Boolean))
    Dim HiFlowPump3 As New DataColumn("Pump" & vbCrLf & "(Hi-Flo)", GetType(Boolean))
    Dim Flow3 As New DataColumn("Flow" & vbCrLf & "(CCM)", GetType(Integer))
    Dim FlowDev3 As New DataColumn("Flow Deviation" & vbCrLf & "          (%)", GetType(Integer))
    Dim TankTemp3 As New DataColumn("Tank Fluid" & vbCrLf & "     (°F)", GetType(Integer))
    Dim ExitFluidLowLim3 As New DataColumn("Exit Fluid LO-LIM" & vbCrLf & "            (°F)", GetType(Integer))
    Dim PartTempLim3 As New DataColumn("Part HI-LIM" & vbCrLf & "       (°F)", GetType(Integer))
    Dim TorqueLim3 As New DataColumn("Torque HI-LIM" & vbCrLf & "       (Nm)", GetType(Single))
    Dim Divert3 As New DataColumn("Divert Oil", GetType(Boolean))
    Dim DivertTimeOn3 As New DataColumn("Diverter On" & vbCrLf & "        (S)", GetType(Integer))
    Dim DivertTimeOff3 As New DataColumn("Diverter Off" & vbCrLf & "        (S)", GetType(Integer))
Related