【发布时间】:2016-05-01 02:43:23
【问题描述】:
我正在向我的 DataGridView 添加ComboBoxColumn,并使用 BindingSources 将其链接到单独的相关部门表。
新行显示第一个列出的部门(Accounts),但默认不选中,导致错误:
无法将值 NULL 插入列“DeptID”,表 'StaffDB.dbo.Staff';列不允许空值。插入失败。
根据答案 here at SO 的建议,我使用以下设置默认值:
comboCol.DefaultCellStyle.NullValue = _bsDept(0)("Department")
comboCol.DefaultCellStyle.DataSourceNullValue = _bsDept(0)("DeptID")
这似乎适用于调试,值为 4 和“帐户”,但仍然产生相同的错误。
以下是 ComboBox 的所有相关设置:
Dim comboCol = New DataGridViewComboBoxColumn()
comboCol.DataSource = _bsDept
comboCol.ValueMember = "DeptID"
comboCol.DisplayMember = "Department"
'staff table..
comboCol.DataPropertyName = "DeptID"
comboCol.HeaderText = "Department"
comboCol.DefaultCellStyle.Format = "d"
'the first Department "Accounts" appears to be selected, but it isn't
comboCol.DefaultCellStyle.NullValue = _bsDept(0)("Department")
comboCol.DefaultCellStyle.DataSourceNullValue = _bsDept(0)("DeptID")
dgvStaff.Columns.Add(comboCol)
为什么我的默认值被忽略了? (如果我自己选择一个部门,组合框就可以工作。)
【问题讨论】:
-
我不确定那些
DefaultCellStyle道具是否能控制 Null 等的显示方式。有一个DefaultValuesNeeded事件可能更合适 -
谢谢,这有点道理;)。如果您将其添加为答案,我可以接受,请在活动中使用此代码
e.Row.Cells("Department").Value = _bsDept(0)("DeptID")。
标签: vb.net winforms datagridview