1

So, I'm trying to implement a ComboBoxColumn but it's not saving the data unlike when I edit the other elements. I can choose the elements inside the ComboBox during run but it's not being saved. Even sorting the other rows makes the ComboBox empty again.

XAML:

...

<DataGridComboBoxColumn x:Name="ColSID" Header="Guild"
                        SelectedValueBinding="{ Binding Guilds, Mode=TwoWay }"
                        ItemsSource="{Binding Guilds}"
                        SelectedValuePath="SID"
                        DisplayMemberPath="Name" 
                        CanUserSort="False" />
...

Here is the Guild class:

public class Guild
{
    public string SID { get; set; }
    public string Name { get; set; }
}

Initializing via:

Guilds = new List<Guild>();
Guilds.Add(new Guild { SID="1", Name="Test Server x1" });
Guilds.Add(new Guild { SID="2", Name="Test Server x2" });
ColSID.ItemsSource = Guilds;

The DataTable is stored into a JSON file as soon as a cell is changed:

DataTable dt = ((DataView)senderDataGrid.ItemsSource).ToTable();
using (StreamWriter file = File.CreateText(filename))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(file, dt);
}
FadedCoder
  • 1,517
  • 1
  • 16
  • 36
  • Why do you have binding to `ItemSource` if the code-behind sets it? And where the `Guilds` in `SelectedValueBinding` come from? – Chayim Friedman Aug 29 '19 at 09:06
  • @ChayimFriedman `ItemSource` in XAML is...redundant code, didn't notice it. And I'm not exactly sure what `SelectedValueBinding` should contain, new to WPF. – FadedCoder Aug 29 '19 at 09:16
  • I've never worked with WPF dgcb either, but in winforms DGVCB you typically have two tables/data sources: one is the table (let's use datatable as an example, called Data) that contains all the data including the ID value that your combo is supposed to set, eg a column called SID, with values like 1, 2.. and in the other table a decoding of key/value pairs, lets assume a datatable called Servers that has ID and Name.. You set the combo's items datasource to be the Servers table, the SelectedValue is binded to Data.SID, the combo has valuemember of Servers.ID and a Displaymember of Servers.Name – Caius Jard Aug 29 '19 at 09:32
  • 1
    The reason I say all this is it looks like your combo is binded to the same table it gets its item data from, which kinda isnt the way i'd expect it to work. A combo is binded to the Data table, but uses the Servers table as its item list, and whatever id number it finds in servers.ID, it pokes into Data.SID column when the user changes it. When deciding what to show, it auto sets itself to show the text in servers.name based on the Data.SID value it looks up in Servers.ID. Does it make sense? – Caius Jard Aug 29 '19 at 09:33
  • @CaiusJard That's why I asked what's `Guilds`. – Chayim Friedman Aug 29 '19 at 09:39

1 Answers1

1

Your binding should work provided that the DataTable, i.e. the ItemsSource of the senderDataGrid, has a column named "Guilds" that accepts a string value:

SelectedValueBinding="{Binding Guilds, Mode=TwoWay}"

The selected SID will be stored in the column specified by the SelectedValueBinding, provided that there actually is such a column in the DataTable of course.

If it doesn't work, you should either change "Guilds" to the actual name of the column in the DataTable or add the column.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks, this fixed it. Didn't know what exactly `SelectedValueBinding` did, too new to C#. The column was named `SID` so had to change it to `SelectedValueBinding="{Binding SID, Mode=TwoWay}"` – FadedCoder Aug 29 '19 at 11:48