Click here to Skip to main content
15,883,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a DataGrid with a ComboBoxColumn . the ComboBox is getting populated . but the selected value is not getting updated Back to the DataBase.here 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
C#
<Window x:Class="Combowithoutdg.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="218"
                HorizontalAlignment="Left" Margin="9,6,0,0"
                Name="dataGrid1" VerticalAlignment="Top"
                Width="486" >
            <DataGrid.Columns>
            <DataGridTextColumn Header="UnitSlNo" Binding="{Binding Path=UNITSLNO}"/>
                <DataGridComboBoxColumn Header="Options" SelectedValueBinding="{Binding SelectedOption1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="OptionId"                               DisplayMemberPath="OptionText"  >

                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Options}"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Options}"/>
                           
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <!--<DataGridTemplateColumn Header="Option">
                    <DataGridTemplateColumn.CellTemplate>
                        
                        <DataTemplate>
                            <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" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>-->


            </DataGrid.Columns>

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


HTML
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
            {
                get
                {
                    return _selectedOption1;
                }
                set
                {
                    if (_selectedOption1 != value)   //  <--- put breakpoint here to prove it is updating source
                    {
                        _selectedOption1 = value;
                    }
                }
            }

            int _selectedOption2;
            public int SelectedOption2
            {
                get
                {
                    return _selectedOption2;
                }
                set
                {
                    if (_selectedOption2 != value)
                    {
                        _selectedOption2 = value;   //  <--- put breakpoint here to prove it is updating source
                        OnPropertyChanged("SelectedOption2");
                    }
                }
            }

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

            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";

            try
            {
                connection.Open();
                oledbAdapter = new OleDbDataAdapter(sql, connection);
                oledbAdapter.Fill(ds);
                //dataGrid1.DataContext = dt;
                dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
                

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // for updating the DataBase this code is not working for comboBox column;
            try
            {
                oledbCmdBuilder = new OleDbCommandBuilder(oledbAdapter);
                changes = ds.GetChanges();
                if (changes != null)
                {
                    oledbAdapter.Update(ds.Tables[0]);
                }
                ds.AcceptChanges();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


        }
    }
Posted
Updated 18-Feb-12 7:06am
v2
Comments
Manfred Rudolf Bihy 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900