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

72 Views Asked by At

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

PGET_LST

Here is Full view of :MyDataGrid

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

UPDATE in 2023-10-17 : INotifyPropertyChanged

I fill my ObservbleCollection in this way :

 var QRE_KHZ_DATA = dbms.DoGetDataSQL<PGET_LST>($"SELECT ID, DATE, RADIF, NO_AM, NAHVA, FHES_K, FHES_M, FHES_T, THES_K, THES_M, THES_T, SHARH, MABL, N_SERI, BANK, IDH, FHES, THES, ARZD, FHES_T2, THES_T2, FHES_T3, THES_T3, FHES_T4, THES_T4, CRT, UID FROM dbo.PGET_LST WHERE ID = {ID.Text}").ToList();
    
    KHAZANEH_DATA?.Clear();
    for (int i = 0; i < QRE_KHZ_DATA.Count; i++) KHAZANEH_DATA.Add(QRE_KHZ_DATA[i]);

here is the data : Data

1

There are 1 best solutions below

5
mm8 On

Your PGET_LST class should implement the INotifyPropertyChanged interface and raise the PropertyChanged event for each property that you set dynamically.

An ObservableCollection<T> doesn't raise any change notifications when properties of individual items in the collection are set. It raises events when items are added or removed.