Home > Software design >  Why is the desired data not displayed after changing the value in the row of a column in the datagri
Why is the desired data not displayed after changing the value in the row of a column in the datagri

Time:10-12

I have a DataGrid in the C# WPF project and DataGrid's Itemsource fill from the code behind I set a default value for one of my columns but it does not display it until I double click on the column! also in CellEndEdit I test to change the value of the next column, but it dint show it in real time until I double click it (Click to Edit it)

What have I tried after changing data:

 DGR_SUB_INVOLST.Items.Refresh();
 DGR_SUB_INVOLST.BeginEdit();
 DGR_SUB_INVOLST.CommitEdit();
 DGR_SUB_INVOLST.InvalidateVisual();
 DGR_SUB_INVOLST.UpdateLayout();

How do I do that :

XAML:

 <DataGrid x:Name="DGR_SUB_INVOLST"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  ScrollViewer.CanContentScroll="False"
                  ItemsSource="{Binding ALL_DATA_INVO, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                   AutoGenerateColumns="False"  FlowDirection="RightToLeft" CellEditEnding="DGR_SUB_INVOLST_CellEditEnding" SelectionUnit="Cell">
            <DataGrid.Columns >
                <DataGridTextColumn Header="ردیف" x:Name="RADIF_COL"  Binding="{Binding RADIF}" MinWidth="40" Visibility="Hidden"/>

                <DataGridComboBoxColumn x:Name="ANBAR_COL" Width="80" Header=" انبار " SelectedValueBinding="{Binding CODE,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="NAMES" SelectedValuePath="CODE">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="IsEditable" Value="True"/>
                            <Setter Property="ItemsPanel" Value="{StaticResource VSP}"/>
                            <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
                            <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
                            <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>


                <DataGridTextColumn Header="نام کالا" x:Name="NAME_COL" Binding="{Binding NAME,UpdateSourceTrigger=PropertyChanged}" Visibility="Visible" MinWidth="180"/>
                <DataGridTextColumn Header="کد کالا" x:Name="CODEKALA_COL" Binding="{Binding CODEKALA,UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden" MinWidth="80"/>


            </DataGrid.Columns>
        </DataGrid>

C#:

private void DGR_SUB_INVOLST_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (DGR_SUB_INVOLST != null)
    {
        if (DGR_SUB_INVOLST.Items.Count > 0)
        {
            (e.Row.Item as INVOMonitor).CODE = 1;
            //Refresh here :
            DGR_SUB_INVOLST.Items.Refresh();
            DGR_SUB_INVOLST.BeginEdit();
            DGR_SUB_INVOLST.CommitEdit();
            DGR_SUB_INVOLST.InvalidateVisual();
            DGR_SUB_INVOLST.UpdateLayout();
        }
    }
}

Result: enter image description here in fact the items row is really changed but it didn't show!

non of them work! please guide me

CodePudding user response:

I think you should modify the Cell like this :

1- Getting Column and Row Index :

    private void DGR_SUB_INVOLST_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        if (DGR_SUB_INVOLST != null)
        {
            if (DGR_SUB_INVOLST.Items.Count > 0)
            {
                OldRowitem = (INVOMonitor)DGR_SUB_INVOLST.CurrentItem;
                DataGridColumn col1 = e.Column;
                DataGridRow row1 = e.Row;
                OldRowindex = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
                OldColumnindex = col1.DisplayIndex;
            }
        }
    }

2- A method to get Cell :

private DataGridCell GetCellVal(string NAME_SUTUN,int ROWINEX)
    {
        var TheCol = DGR_SUB_INVOLST.Columns.FirstOrDefault(c => c.SortMemberPath == NAME_SUTUN).DisplayIndex;
        var DGCInf = new DataGridCellInfo(DGR_SUB_INVOLST.Items[ROWINEX], DGR_SUB_INVOLST.Columns[TheCol]);
        var TheDGCell = PublicVRB.GetDataGridCell(DGCInf);
        return TheDGCell;
    }

How to use :

private void DGR_SUB_INVOLST_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (DGR_SUB_INVOLST != null)
    {
        if (DGR_SUB_INVOLST.Items.Count > 0)
        {
               (e.Row.Item as INVOMonitor).CODE  = SomeValue;
     ((TextBlock)GetCellConfig("Binding_Name", row_index).Content).Text = (e.Row.Item as INVOMonitor).CODE.ToString();
        }
    }
}
  • Related