DataGridComboBoxColumn won't update for new Item to Display it in C# WPF

Mojtaba_Hakim 281 Reputation points
2023-10-05T19:06:34.8333333+00:00

I'm using C# WPF .NET Core 6 with Visual Studio 2022, I have DataGrid in my window that Binds to an ObservleCollection

XAML :

<DataGrid x:Name="PGET_LST_SUB" AutoGenerateColumns="False"    FlowDirection="RightToLeft" ItemsSource="{Binding KHAZANEH_DATA}" CellEditEnding="PGET_LST_SUB_CellEditEnding">
            <DataGrid.Columns>
                <DataGridComboBoxColumn x:Name="FHES_COLUMN" Width="auto" MinWidth="90" Header="از حساب" Visibility="Visible" SelectedValueBinding="{Binding FHES, UpdateSourceTrigger=LostFocus}" DisplayMemberPath="NAME_FHES">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="IsEditable" Value="True"/>
                            <Setter Property="ItemsPanel" Value="{StaticResource VSP}"/>
                            <Setter Property="VirtualizingPanel.IsVirtualizing" Value="True"/>
                            <Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/>
                            <Setter Property="ItemContainerStyle">
                                <Setter.Value>
                                    <Style TargetType="ComboBoxItem">
                                        <Setter Property="ContentTemplate">
                                            <Setter.Value>
                                                <DataTemplate>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding NAME_FHES}" TextWrapping="WrapWithOverflow" TextAlignment="Justify" Width="300" />
                                                        <TextBlock Text="{Binding FHES}" TextAlignment="Center" Width="100" />
                                                    </StackPanel>
                                                </DataTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding NAME_FHES}" TextWrapping="NoWrap" TextAlignment="Justify" Width="250" />
                                            <TextBlock Text="{Binding FHES}" TextAlignment="Center" Width="100" />
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>

C#:

 public ObservableCollection<PGET_LST> KHAZANEH_DATA { get; set; } = new ObservableCollection<PGET_LST>();

1

Here is Full view : Untitled

My problem is in PGET_LST_SUB_CellEditEnding when I set new item for DataGrid's item it won't display new item of ComboBoxColumn in UI , when I open drop down list is empty and there is no selectedvalue.

private void PGET_LST_SUB_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
 {
    CURRENT_ITMES_ROW = e.Row.Item as PGET_LST;
    if (e.Column.SortMemberPath == "FHES")
    {
         FROM_SEARCH.HES = "115-1-959";
            FROM_SEARCH.NAME = "Jack Walker";
            CURRENT_ITMES_ROW.FHES = FROM_SEARCH.HES;
            CURRENT_ITMES_ROW.NAME_FHES = FROM_SEARCH.NAME;
    }
 }

so I to fix that I have to Reset the ItemSource of that Column:

//Resetting the Itemsource to update UI
FHES_COLUMN.ItemsSource = null;
FHES_COLUMN.ItemsSource = KHAZANEH_DATA.Select(item => new { item.FHES, item.NAME_FHES }).ToList();

I don't think this is the right way, please guide me what should I do according to my needs.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
769 questions
{count} votes