I'm trying to get the .SelectedValue of a ComboBox, but it keeps returning null instead of the actual value in .ValueMember. I tried a couple of different approaches, and came up with this:
DataTable datatable = DataAccess.GetDataTable(DateTime.Now); // Get DataTable from DB call
if (datatable.Rows.Count > 0)
{
List<object> items = new List<object>();
foreach (DataRow row in datatable.Rows)
{
items.Add(new { Text = Convert.ToString(row["PersFullName"]), Value = Convert.ToString(row["PersNbr"]) });
}
this.cmbDDLFilter.DisplayMember = "Text";
this.cmbDDLFilter.ValueMember = "Value";
this.cmbDDLFilter.DataSource = items;
}
This will work when setting the visual part of the ComboBox, but when I do this:
long nbr = Convert.ToInt64(this.cmbDDLFilter.SelectedValue);
The .SelectedValue will be null even though I set the .ValueMember to "Value".
I notice though that the .SelectedText is also null, but the .SelectedItem does indeed have the object I added to the list.
How do I get the anonymous object's .Value to actually work with .SelectedValue?
I've used this SO question as a reference, but it doesn't work for me.