WPF DataGrid ComboBoxColumn Not Updating Bound Data

knutter539 Source

I have a dropdown column in my datagrid, the options for the combobox are stored in the tmfCNCComponentStatus_threed table. My main table has a column called [Status] which corresponds to the key column in that table.

The correct status [Description] is displayed in the combobox for each row in my datagrid, but when the user changes the selection, the database isn't updating, even though everything looks as though it is working properly. I have the "UpdateSourceTrigger" set to PropertyChanged as seen in many similar posts but still no dice. Any insight would be greatly appreciated!

<CollectionViewSource x:Key="StatusItems"/>

                               <DataGridTemplateColumn x:Name="StatusColumn" Header="Status" Width="*" IsReadOnly="False">
                        <TextBlock x:Name="cboStatus" Text="{Binding Path=Description}"/>
                        <ComboBox x:Name="StatusCombo" SelectedValuePath="CNCComponentStatusKey" DisplayMemberPath="Description" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource StatusItems}}" IsEditable="True" IsSynchronizedWithCurrentItem="True"/>

The code behind:

Dim com As String = "SELECT tmfCNCComponent_threed.[CNCComponentKey]
    FROM [test_3DimensionalDB].[dbo].[tmfCNCComponent_threed]
    INNER JOIN tmfCNCComponentStatus_threed
    ON tmfCNCComponent_threed.Status = tmfCNCComponentStatus_threed.CNCComponentStatusKey 
    WHERE [ComponentDescription] " & component & " 'trode%' AND [CompanyID]='" & company & "' AND [Status]" & status & "ORDER BY [UpdateTime] DESC"

    Dim Adpt As New SqlDataAdapter(com, con)
    Dim ds As New DataSet()
    Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
    dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView


    Dim statusCVS As CollectionViewSource = FindResource("StatusItems")

    Dim com2 As String = "SELECT * FROM tmfCNCComponentStatus_threed"
    Dim AdptStatus As New SqlDataAdapter(com2, con)
    AdptStatus.Fill(ds, "dbo.tmfCNCComponentStatus_threed")

    Dim statusRows = ds.Tables("dbo.tmfCNCComponentStatus_threed").Rows
    Dim statuses As New List(Of Object)

    For Each row As DataRow In statusRows
        statuses.Add(New With {
        .Status = CInt(row("CNCComponentStatusKey")),
        .Description = CStr(row("Description"))

    statusCVS.Source = statuses


Thanks for your time.

The Database:

enter image description here

Contents of the Status table: enter image description here

The Datagrid: enter image description here

Here is the first part of this question that was addressed yesterday to get me to this point: Part 1



answered 4 months ago Ed Plunkett #1

Based on information in comments in a different question, you probably need to change SelectedValuePath="Status" to SelectedValuePath="CNCComponentStatusKey". The names of the columns or properties of the items in the combobox are critical to answering this question, and you haven't provided them.

The grid will update the DataRowView column value when the cell leaves edit mode.

Mode=TwoWay on that binding is unnecessary. That's the default mode for bindings on ComboBox.SelectedValue.

You can remove all those decorations from the binding on TextBlock.Text: It can't update the source at all, when you think about it, so there's no need to clutter up your XAML with elaborate instructions about how and when it should do something it can't do anyway.

Complete working example

This is the code with which I tested the above answer. It updates the rows in the main table. It doesn't save the table to a database. That's a separate thing.

I don't know if your columns aren't called what you think they are, or what.


    public MainWindow()


    #region Lists
    private static List<String> _status = new List<String>
        "Not Ready",

    private static List<String> _words = new List<String>
    #endregion Lists

    protected void LoadData()
        DataTable dtMain = new DataTable();

        dtMain.Columns.Add("Status", typeof(int));
        dtMain.Columns.Add("Programmer", typeof(String));

        _words.ForEach(w =>
            var row = dtMain.NewRow();
            row[0] = ((int)w[0] % 2) + 1;
            row[1] = w;

        DataTable dtStatus = new DataTable();

        dtStatus.Columns.Add("CNCComponentStatusKey", typeof(int));
        dtStatus.Columns.Add("Description", typeof(String));

        _status.ForEach(s =>
            var row = dtStatus.NewRow();
            row[0] = dtStatus.Rows.Count + 1;
            row[1] = s;

        DataGrid.ItemsSource = dtMain.DefaultView;

        var cvs = (FindResource("StatusItems") as CollectionViewSource);

        cvs.Source = dtStatus.DefaultView;


    <CollectionViewSource x:Key="StatusItems" />
    <DataGrid x:Name="DataGrid" AutoGenerateColumns="False">
            <DataGridTextColumn Binding="{Binding Programmer}" Header="Programmer" />
            <DataGridTemplateColumn Header="Status">
                        <TextBlock Text="{Binding Status}"/>
                        <StackPanel Orientation="Horizontal">
                                ItemsSource="{Binding Source={StaticResource StatusItems}}"
                                SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}"
                            <!-- Selected value in combo -->
                            <Label Content="{Binding SelectedValue, ElementName=Combo}" />
                            <!-- Value of Status column in row -->
                            <Label Content="{Binding Status}" />

comments powered by Disqus