This is a migrated thread and some comments may be shown as answers.

Changed in RadComboBox doesn't update database

0 Answers 116 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Veteran
Martin asked on 10 Jun 2020, 02:52 AM

I have created a radcombobox inside my radgridview and bound some text to it which works fine, but I can't seem to get it to update the database for the selected row when I change the selection of the combo box?

 

My XAML:

<telerik:GridViewDataColumn Header="Produktgruppe" Width="Auto" MinWidth="180" IsReadOnly="True">
                    <telerik:GridViewDataColumn.CellStyle>
                        <Style TargetType="{x:Type telerik:GridViewCell}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type telerik:GridViewCell}">
                                        <telerik:RadComboBox ItemsSource="{Binding Source={StaticResource ViewModelProductGroups}, Path=ProductGroups}"
                                                             DisplayMemberPath="Name"
                                                             IsEditable="True"
                                                             IsReadOnly="True"
                                                             SelectedIndex="0"
                                                             EmptyText="Vælg produktgruppe"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </telerik:GridViewDataColumn.CellStyle>
                </telerik:GridViewDataColumn>

 

My code-behind:

public Products()
        {
            InitializeComponent();
        }
 
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            using (Context dbContext = new Context())
            {
                GvProducts.ItemsSource = dbContext.Product.Include(x => x.ProductGroup).ToList();
            }
        }
 
        private void GvProducts_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            if (e.NewData is Models.Product editedProduct)
            {
                using (Context dbContext = new Context())
                {
                    dbContext.Entry(editedProduct).State = EntityState.Modified;
                    dbContext.SaveChanges();
                }
            }
        }

 

My view model

sdf

public ObservableCollection<Models.ProductGroup> ProductGroups { get; set; }
        public ViewModelProductGroups()
        {
            using (Context dbContext = new Context())
            {
                ProductGroups = new ObservableCollection<Models.ProductGroup>(dbContext.ProductGroup.ToList());
            }
        }

 

Any suggestions?

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Martin
Top achievements
Rank 1
Veteran
Share this question
or