问题
I'm currently working with C# and I have the following code to set a ComboBox column inside a DGV:
// Add the values of the combo box
dgvcbGeneric.DataSource = dtDataSource;
dgvcbGeneric.ValueMember = "ID";
dgvcbGeneric.DisplayMember = "Value";
dgvcbGeneric.DataPropertyName = strColumn;
dgvcbGeneric.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dgvcbGeneric.DisplayStyleForCurrentCellOnly = true;
// vGenericCMB.SortMode = DataGridViewColumnSortMode.Automatic
dgvcbGeneric.DefaultCellStyle.NullValue = columns[strActualColumn].nullFormat;
// Add the new ComboBoxColumn of the DGV
dgvLink.Columns.Add(dgvcbGeneric);
All works fine, but the DGV only show the member value when the cell has the focus:
Someone can explain me why this issue is happening?
回答1:
I found the error by myself. Basically in the DataSource of the DGV the column "Day" was of type "Byte" (tinyint in SQL). When the code try to create the DataTable to fill up the DGV ComboBox Column; the ID type is "Integer".
The solution was check the DataType of the ID vs the column source. If are differents; throw an exception to check previously.
来源:https://stackoverflow.com/questions/30384684/cells-show-the-value-member-id-instead-display-member