单元格显示值成员(ID),而不是显示成员 [英] Cells show the value member (ID) instead Display member
问题描述
/ /添加组合框的值
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;
//添加DGV的新ComboBoxColumn
dgvLink.Columns.Add(dgvcbGeneric);
所有功能都可以正常工作,但是当单元格有焦点时,DGV仅显示成员值:
有人可以解释为什么会出现这个问题?
我自己发现错误。基本上在DGV的DataSource中,Day列的类型为Byte(SQL中为tinyint)。
当代码尝试创建DataTable以填充DGV ComboBox列; ID类型为整数。
解决方案是检查ID与Data Source的DataType。如果是不同的;抛出异常以检查以前的检查。
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?
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.
这篇关于单元格显示值成员(ID),而不是显示成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!