user7157732
user7157732

Reputation: 347

WPF Datagrid cell value edit - C#

I have a simple datagrid with 2 columns that I populate using an observable collection. The requirement is to enable cell edit for one of the columns and then using a button, save the column data somewhere. This is how I have implemented it so far:

View Model:

 public class PlanningResult : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            public string ProducerName { get; set; }
            public string leasename { get; set; }
}

Observable Collection:

ObservableCollection<PlanningResult> populatePatternData = new ObservableCollection<PlanningResult>();
public ObservableCollection<PlanningResult> PopulatePatternData
            {
                get { return populatePatternData; }
                set
                {
                    populatePatternData = value;
                    base.OnPropertyChanged("StringList");
                }
            }

XAML : I set the "IsReadOnly=False" for the property ProducerName , hence allowing user to update this value if required.

 <DataGrid x:Name="PrintReport" ItemsSource="{Binding PopulatePatternData}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="12" CanUserSortColumns="False"
                                                     HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  AlternatingRowBackground="Gainsboro"  AlternationCount="1" 
                                                     SelectionMode="Extended" SelectionUnit="Cell" >

                                            <DataGrid.Columns>
                                                    <DataGridTextColumn Header="Pattern" Binding="{Binding ProducerName}" IsReadOnly="False"  >
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style>
                                                            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                                            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                    <DataGridTextColumn Header="Lease" Binding="{Binding leasename}" IsReadOnly="True" >
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style>
                                                            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                                            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>

My question is what is the next step in terms of how to "get the updated values of the column (ProducerName)" and re-populate the observable collection?

Upvotes: 0

Views: 2275

Answers (1)

rmojab63
rmojab63

Reputation: 3629

Use a TwoWay mode Binding:

Binding="{Binding ProducerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

This will update the specific item in the ObservableCollection, whenever the user changes the value of the cell.

Furthermore, use commanding in order to save the current state of your ObservableCollection. See this among many other answer and articles.

Upvotes: 1

Related Questions