I’m trying to get something set up in a DataGridView. It seems like this should be pretty straightforward but I’m having trouble. I want to display three columns:
- CodeID
- CodeName
- ComboBox with DisplayMember of TypeName, ValueMember of TypeID
I want to be able to select from all possible values of TypeName. Here’s my dilemma:
If I load all of this into one DataTable and set the DataGridView as the DataSource, I can display the existing TypeName for that record, but the combo box will not include any other values. If I set the DataSource for the DataGridViewComboBoxColumn to a separate DataTable that includes all possible TypeNames, the existing value is not displayed.
DataGridView is really annoying to work with so either a solution for this or a viable alternative would be appreciated.
Edit: it appears the issue is caused by my wanting to have a separate item for DisplayMember and ValueMember. The following works, if I don’t worry about setting the ID as the ValueMember:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "Type",
DataPropertyName = "Type"
}
If I do the following, the right types are selected, but I can’t change the selection in the combo box:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "TypeID"
}
If I use the following I get a FormatException error as it’s trying to populate:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "Type"
}
edit: typeList is a simple DataTable populated by the following:
SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]
FROM InsuranceType IT
WHERE IT.ClientID = @ClientID
ORDER BY [Type]
Ok, I came up with an example
ClientInfoandInsuranceDetailsthat I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate theDataGridViewComboBoxand set the value based on theInsuranceDetails(specifically at:InsurDetailz = all_insurance_types[2])