Search code examples
wpfxamlbindingdatagrid

Binding the property name is not loading but others are from the grid


I am using the correct binding name for my property as it matches the one in the table from the database, other names do show and load data. What else could be the problem on my binding this name RM_Code?

enter image description here

// xaml code for binding property name

`<Label Content="Micros" FontWeight="SemiBold" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Top" Width="950" BorderBrush="#FFBEBEBE" Background="#FFE3E3E3" BorderThickness="1.5" Foreground="Black" Height="35" VerticalContentAlignment="Center" FontSize="14" Margin="10,350,0,0" Grid.Row="1"/>
        <DataGrid x:Name="dgvMicroRecipe" VerticalScrollBarVisibility="Visible" SelectedCellsChanged="dgvMacroRecipe_SelectedCellsChanged" CellEditEnding="dgvMacroRecipe_CellEditEnding" VerticalAlignment="Top" HorizontalGridLinesBrush="#FFBBBBBB" AutoGenerateColumns="False" CanUserDeleteRows="False" HeadersVisibility="Column" GridLinesVisibility="Horizontal" IsReadOnly="True" FontWeight="Normal" BorderBrush="#FF767676" Margin="10,380,0,0" HorizontalAlignment="Left" FontSize="12" Width="950" Grid.Row="1">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Blue" />
                            <Setter Property="BorderBrush" Value="Blue"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontWeight" Value="SemiBold"/>
                    <Setter Property="TextElement.FontSize" Value="14"/>
                    <Setter Property="Background" Value="#FF383838"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="SeparatorVisibility" Value="Visible"/>
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding idx1}" Header="IDX" Width="50" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding [RM_Code]}" Header="RM Code" Width="Auto" IsReadOnly="True" FontSize="8">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn  Visibility="Visible" Width="5">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding [Stock Code]}" Header="Stock Code" Width="100" IsReadOnly="True" FontWeight="SemiBold">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Style.Triggers>
                                <Trigger Property="Text" Value="Yes">
                                    <Setter Property="Background" Value="LightGreen"/>
                                </Trigger>
                                <Trigger Property="Text" Value="No">
                                    <Setter Property="Background" Value="#FFFF6969"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding [Stock Description]}" Header="Stock Description" Width="150" IsReadOnly="True" FontWeight="SemiBold">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Style.Triggers>
                                <Trigger Property="Text" Value="Yes">
                                    <Setter Property="Background" Value="LightGreen"/>
                                </Trigger>
                                <Trigger Property="Text" Value="No">
                                    <Setter Property="Background" Value="#FFFF6969"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding [Percentage]}" Header="Percentage" Width="100" IsReadOnly="True" FontWeight="SemiBold">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Style.Triggers>
                                <Trigger Property="Text" Value="Yes">
                                    <Setter Property="Background" Value="LightGreen"/>
                                </Trigger>
                                <Trigger Property="Text" Value="No">
                                    <Setter Property="Background" Value="#FFFF6969"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Kilograms}" Header="Kilograms" Width="100" IsReadOnly="True" FontWeight="SemiBold">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>

                <!-- New Columns Added -->
                <DataGridTextColumn Binding="{Binding BagWeight}" Header="Bag-Weight" Width="100" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding FullBagUsed}" Header="Full Bag Used" Width="100" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding KgPartBagUsed}" Header="Kg Part Bag Used" Width="150" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalKgUsed}" Header="Total Kg Used" Width="100" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

`

I have changed all different names for this binding property but still it was not even loading the data from the back end like other columns as their names conversions are correct. What else could i have done to resolve it?


Solution

  • Try using this

    Binding="{Binding RM_Code, Mode=TwoWay}"
    

    instead of

    Binding="{Binding [RM_Code]}"
    

    This should fix your problem.