Click here to Skip to main content
11,486,023 members (75,878 online)
Rate this: bad
Please Sign up or sign in to vote.
i have a DataGrid with a ComboBoxColumn . the ComboBox is getting populated . but the selected value is not getting updated Back to the my ultimate goal is to bind selected value of this DataGridComboBox column back to a column named "OPTIONS" in the MS Access DataBase. each time when the DataGrid loads it should show last updated values in all columns .but here the updation is not occuring for the DataGridComboBoxColumn. I have manually added some text in the OPTIONS column in the MS Access DataBase but unfortunately it is not populated when the DataGrid load . That means DataBase and DataGrid is not communicating proprly each other. I am posting the entire code
<Window x:Class="Combowithoutdg.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <DataGrid AutoGenerateColumns="False" Height="218"
                HorizontalAlignment="Left" Margin="9,6,0,0"
                Name="dataGrid1" VerticalAlignment="Top"
                Width="486" >
            <DataGridTextColumn Header="UnitSlNo" Binding="{Binding Path=UNITSLNO}"/>
                <DataGridComboBoxColumn Header="Options" SelectedValueBinding="{Binding SelectedOption1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="OptionId"                               DisplayMemberPath="OptionText"  >
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Options}"/>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Options}"/>
                <!--<DataGridTemplateColumn Header="Option">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Width="30" Text="{Binding ElementName=mycombo, Path=SelectedValue}" removed="Yellow" />
                                <ComboBox x:Name="mycombo"
								 ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Options}"
								 SelectedValue="{Binding SelectedOption2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="OptionId" DisplayMemberPath="OptionText" />

        <Button Content="Button" Height="33"
                Margin="97,268,0,0" Name="button1"
                VerticalAlignment="Bottom" Width="83"
                Click="button1_Click" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Combowithoutdg
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        OleDbConnection connection;
        OleDbDataAdapter oledbAdapter;
        DataTable dt = new DataTable();
        OleDbCommandBuilder oledbCmdBuilder;
        DataSet ds = new DataSet();
        DataSet changes;
        int i = 0;
        string sql;
        string connectionstring;
        string data = null;
        public class DbOption
            public int OptionId { get; set; }
            public string OptionText { get; set; }

        public class DbCustomer : INotifyPropertyChanged
            public string CustomerName { get; set; }
            int _selectedOption1;
            public int SelectedOption1
                    return _selectedOption1;
                    if (_selectedOption1 != value)   //  <--- put breakpoint here to prove it is updating source
                        _selectedOption1 = value;
            int _selectedOption2;
            public int SelectedOption2
                    return _selectedOption2;
                    if (_selectedOption2 != value)
                        _selectedOption2 = value;   //  <--- put breakpoint here to prove it is updating source
            void OnPropertyChanged(string prop)
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(prop));
            public event PropertyChangedEventHandler PropertyChanged;
        public List<DbCustomer> Customers { get; set; }
        public List<DbOption> Options { get; set; }


        public MainWindow()
            Options = new List<DbOption>
                new DbOption { OptionId=1, OptionText="Option 1" },
                new DbOption { OptionId=2, OptionText="Option 2" },
                new DbOption { OptionId=3, OptionText="Option 3" },
            Customers = new List<DbCustomer>
                new DbCustomer { CustomerName="Fred", SelectedOption2=3 },
                new DbCustomer { CustomerName="Jane", SelectedOption1=2 }
            DataContext = this;
            connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Project/A&T.accdb";
            connection = new OleDbConnection(connectionstring);
            sql = "Select*from MC";
                oledbAdapter = new OleDbDataAdapter(sql, connection);
                //dataGrid1.DataContext = dt;
                dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
            catch (Exception ex)
        private void button1_Click(object sender, RoutedEventArgs e)
            // for updating the DataBase this code is not working for comboBox column;
                oledbCmdBuilder = new OleDbCommandBuilder(oledbAdapter);
                changes = ds.GetChanges();
                if (changes != null)
            catch (Exception ex)

Posted 18-Feb-12 8:05am
Edited 18-Feb-12 8:06am
Manfred R. Bihy at 23-Feb-12 13:33pm
Again a code dump. This is not going to be easy for you to get help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 8,047
2 Sascha Lefèvre 3,199
3 Maciej Los 2,886
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 18 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100