I'm adding a
ComboBoxColumn
Cannot insert the value NULL into column 'DeptID', table
'StaffDB.dbo.Staff'; column does not allow nulls. INSERT fails.
comboCol.DefaultCellStyle.NullValue = _bsDept(0)("Department")
comboCol.DefaultCellStyle.DataSourceNullValue = _bsDept(0)("DeptID")
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)
I am not sure those DefaultCellStyle props do more than control how Nulls etc are displayed, given the name. There is a DefaultValuesNeeded
event which might be a more appropriate place to set default values. For instance (some code I have handy that does just that):
Private Sub dgv2_DefaultValuesNeeded(sender As Object,
e As DataGridViewRowEventArgs) Handles dgv2.DefaultValuesNeeded
'defaults
e.Row.Cells("Fish").Value = "Cod"
e.Row.Cells("Bird").Value = "Raven"
e.Row.Cells("itemDate").Value = DateTime.Now.Date
End Sub
In your case, you would supply a value from the Department source.