DataGrid ComboBox Binding Issue with Selected or New Item

None None

I am having issue with retaining selected item in a DataGridTemplate column ComboBox. I have the DataTemplate editable combobox column as the first column in the datagrid and next to it, I have a text column. The DataGrid is populated with data read from SQL stored procedure. Everything works fine, except when I select an item in the combobox and move to the text field and start typing in it, the Combo selection blanks out. It blanks out both for a new item or existing item. Oddly, this happens only the first time. When I reselect the ComboBox Value or add the new item again and go back to the text field, it does not blank out. I am running out of ideas and tried many combinations, but no luck so far. Here is my code:

This is how I am populating the DataGrid:

using (SqlCommand cmd = new SqlCommand())
{
    cmd.CommandText = "GetProducts";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = sqlConn;

    var reader = cmd.ExecuteReader();
    var dt = new DataTable();
    dt.Load(reader);
    dt.Columns["ProductName"].AllowDBNull = true;
    dtProductCfgTable = dt;
    ProductCfgGrid.ItemsSource = dtProductCfgTable.DefaultView;
}

This is the declaration for ProductNamesList:

public List<string> ProductNamesList { get; set; }

XAML:

<DataGridTemplateColumn Header="ProductName">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
        <ComboBox ItemsSource="{Binding ProductNamesList, 
                                RelativeSource={RelativeSource AncestorType=Window}}"  
                                SelectedItem="{Binding ProductName 
                                IsSynchronizedWithCurrentItem="False"  
                                BorderThickness="1.2 1.2 0 0" BorderBrush="Black" 
                                Background="LightCyan" IsEditable="True" />
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>  
<DataGridTextColumn Binding="{Binding ShippingAddress}" 
                    Width="100" 
                    Header="ShippingAddress" 
                    Visibility="Visible"/>
James Sampica

The reason the data is lost is because the CellTemplate only provides non-edit features, so whenever you changed a value in the combobox when editing a new row, the data wasn't getting set because there was no edit-mode implementation, so no object was getting created behind the scenes. DatagridTextColumn automatically has editing build into it, which is why the combobox would work after editing this type of cell.

<DataGridTemplateColumn Header="ProductName" >
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
                 <ComboBox ItemsSource="{Binding ProductNamesList, 
                 RelativeSource={RelativeSource AncestorType=Window}}" 
                 SelectedValue="{Binding ProductName, Mode=TwoWay}"
                 IsSynchronizedWithCurrentItem="False"
                 IsEditable="False"
                 IsHitTestVisible="False" />
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     <DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox ItemsSource="{Binding ProductNamesList, 
                 RelativeSource={RelativeSource AncestorType=Window}}" 
                 Text="{Binding ProductName, Mode=TwoWay}"
                 IsSynchronizedWithCurrentItem="False"
                 IsEditable="True" />
         </DataTemplate>
     </DataGridTemplateColumn.CellEditingTemplate>
 </DataGridTemplateColumn>

The redundancy in comboboxes is only necessary if you want the user to see a combobox when in non-edit mode. If you don't care about that you can simply write:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ProductName}" />
    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Datagrid Combobox Seletected item Value to Textbox binding

From Dev

WPF datagrid binding issue: first row bound but doesn't update selected item in two-way fashion

From Dev

MVVM combobox Selected item binding in listview

From Dev

MVVM combobox Selected item binding in listview

From Dev

Issue binding style of selected item with Aurelia

From Dev

DataGrid ComboBox has no selected item when clicked the first time

From Dev

Binding ComboBox SelectedItem to Datagrid

From Dev

Binding ComboBox to DataGrid Entry

From Dev

ComboBox selected item binding not showing the initial value - then working OK

From Dev

How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns

From Dev

Issue with binding in DataGrid GoupStyle

From Dev

Binding the same Collection to ComboBox and Datagrid

From Dev

Combobox Selected Item

From Dev

Binding DataGrid wth ComboBox (PropertyChanged, WPF, Binding)

From Dev

TextBlock binding to selected DataGrid Element

From Dev

binding selected item angularjs

From Dev

Telerik RadGridView selected item binding not working when clicking on button in datagrid properly

From Dev

Binding Dictionary<String, Int32> by keys to ListBox and selected item values to ComboBox

From Dev

Combobox data binding with item templates

From Dev

Combobox data binding with item templates

From Dev

WPF ComboBox binding issue with objects

From Dev

ComboBox binding - receive notification on ComboBox item change

From Dev

Bind DataGrid to ListBox selected item

From Dev

Getting selected Item in DataGrid in WPF

From Dev

Set selected item in WPF ComboBox

From Dev

Displaying a selected ComboBox Item in WPF

From Dev

Change ComboBox text but not selected item

From Dev

WPF Combobox Selected Item not visible

From Dev

Bind text to selected item in combobox

Related Related

  1. 1

    Datagrid Combobox Seletected item Value to Textbox binding

  2. 2

    WPF datagrid binding issue: first row bound but doesn't update selected item in two-way fashion

  3. 3

    MVVM combobox Selected item binding in listview

  4. 4

    MVVM combobox Selected item binding in listview

  5. 5

    Issue binding style of selected item with Aurelia

  6. 6

    DataGrid ComboBox has no selected item when clicked the first time

  7. 7

    Binding ComboBox SelectedItem to Datagrid

  8. 8

    Binding ComboBox to DataGrid Entry

  9. 9

    ComboBox selected item binding not showing the initial value - then working OK

  10. 10

    How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns

  11. 11

    Issue with binding in DataGrid GoupStyle

  12. 12

    Binding the same Collection to ComboBox and Datagrid

  13. 13

    Combobox Selected Item

  14. 14

    Binding DataGrid wth ComboBox (PropertyChanged, WPF, Binding)

  15. 15

    TextBlock binding to selected DataGrid Element

  16. 16

    binding selected item angularjs

  17. 17

    Telerik RadGridView selected item binding not working when clicking on button in datagrid properly

  18. 18

    Binding Dictionary<String, Int32> by keys to ListBox and selected item values to ComboBox

  19. 19

    Combobox data binding with item templates

  20. 20

    Combobox data binding with item templates

  21. 21

    WPF ComboBox binding issue with objects

  22. 22

    ComboBox binding - receive notification on ComboBox item change

  23. 23

    Bind DataGrid to ListBox selected item

  24. 24

    Getting selected Item in DataGrid in WPF

  25. 25

    Set selected item in WPF ComboBox

  26. 26

    Displaying a selected ComboBox Item in WPF

  27. 27

    Change ComboBox text but not selected item

  28. 28

    WPF Combobox Selected Item not visible

  29. 29

    Bind text to selected item in combobox

HotTag

Archive