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">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="cboStatus" Text="{Binding Path=Description}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="StatusCombo" SelectedValuePath="CNCComponentStatusKey" DisplayMemberPath="Description" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource StatusItems}}" IsEditable="True" IsSynchronizedWithCurrentItem="True"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

The code behind:

Dim com As String = "SELECT tmfCNCComponent_threed.[CNCComponentKey]
    ,tmfCNCComponent_threed.[CompanyID]
    ,tmfCNCComponent_threed.[JobNumber]
    ,tmfCNCComponent_threed.[LogNumber]
    ,tmfCNCComponent_threed.[Customer]
    ,tmfCNCComponent_threed.[DueDate]
    ,tmfCNCComponent_threed.[JobLeader]
    ,tmfCNCComponent_threed.[CADProgrammer]
    ,tmfCNCComponent_threed.[Salesperson]
    ,tmfCNCComponent_threed.[CNCProgrammer]
    ,tmfCNCComponent_threed.[ComponentDescription]
    ,tmfCNCComponent_threed.[ComponentFilePath]
    ,tmfCNCComponent_threed.[Material]
    ,tmfCNCComponent_threed.[ComponentSizeX]
    ,tmfCNCComponent_threed.[ComponentSizeY]
    ,tmfCNCComponent_threed.[ComponentSizeZ]
    ,tmfCNCComponent_threed.[QuantityShown]
    ,tmfCNCComponent_threed.[QuantityMirror]
    ,tmfCNCComponent_threed.[UpdateTime]
    ,tmfCNCComponent_threed.[Status]
    ,tmfCNCComponent_threed.[ProgStarted]
    ,tmfCNCComponentStatus_threed.[Description]
    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)
    con.Open()
    Dim ds As New DataSet()
    Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
    dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView

    con.Close()

    con.Open()
    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"))
    })
    Next

    statusCVS.Source = statuses
    con.Close()


    RowCount()
    searchBox.Clear()

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

wpfvb.netdata-bindingwpfdatagrid

Answers

answered 2 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.

MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();

        LoadData();
    }

    #region Lists
    private static List<String> _status = new List<String>
    {
        "Ready",
        "Not Ready",
        "Weary",
        "Disordered",
    };

    private static List<String> _words = new List<String>
    {
        "Ewwrigowasaus",
        "Skubreuph",
        "Creecroicr",
        "Eunthaudrawtr",
        "Ootwoww",
        "Meuleetroithr",
        "Rewshr",
        "Scoysl",
        "Scewziexul",
        "Kawxayzeec",
    };
    #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;
            dtMain.Rows.Add(row);
        });

        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;
            dtStatus.Rows.Add(row);
        });

        DataGrid.ItemsSource = dtMain.DefaultView;

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

        cvs.Source = dtStatus.DefaultView;
    }

MainWindow.xaml

<Window.Resources>
    <CollectionViewSource x:Key="StatusItems" />
</Window.Resources>
<Grid>
    <DataGrid x:Name="DataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Programmer}" Header="Programmer" />
            <DataGridTemplateColumn Header="Status">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Status}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <ComboBox
                                ItemsSource="{Binding Source={StaticResource StatusItems}}"
                                SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}"
                                DisplayMemberPath="Description"
                                SelectedValuePath="CNCComponentStatusKey"
                                x:Name="Combo"
                                />
                            <!-- Selected value in combo -->
                            <Label Content="{Binding SelectedValue, ElementName=Combo}" />
                            <!-- Value of Status column in row -->
                            <Label Content="{Binding Status}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

comments powered by Disqus