Cells show the value member (ID) instead Display member

╄→尐↘猪︶ㄣ 提交于 2020-01-17 12:24:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!