将datagridViewComboboxColumn中的类型化文本项添加到数据库后出现错误

时间:2015-12-11 07:28:14

标签: vb.net datagridcomboboxcolumn

基本上,我不是用户能够输入datagridViewComboboxColumn 如果没有匹配,它会自动将文本保存到数据库中,更新BindingSource并选择项目。

到目前为止我所拥有的。 bindingsource:

With acctTitleCombo6
    .AutoComplete = True
    Try
        .DataSource = ds4
        .DisplayMember = "desc_description"
        .ValueMember = "desc_id"
        .DataPropertyName = "desc_id"
        .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End With

在EditingControlShowing中将组合框样式更改为下拉列表:

Dim comboBoxColumn As DataGridViewComboBoxColumn = DataGridView2.Columns(0)
If (DataGridView2.CurrentCellAddress.X = comboBoxColumn.DisplayIndex) Then
    Dim cb As ComboBox = TryCast(e.Control, ComboBox)
    If (cb IsNot Nothing) Then
        cb.DropDownStyle = ComboBoxStyle.DropDown
    End If
End If

检查输入的文本是否已存在于源项目中(CellValidating事件)。

Dim comboBoxColumn As DataGridViewComboBoxColumn = DataGridView2.Columns(0)
cs2.Open()
Dim comm, comm2, comm3 As New MySqlCommand
comm.Connection = cs2
Dim msgAddDesc As String = "Your description is currently not in the list." & vbNewLine & _
    "Would you like to add '"

If (e.ColumnIndex = comboBoxColumn.DisplayIndex) And Not String.IsNullOrWhiteSpace(e.FormattedValue) Then
    Dim itemIE As IEnumerator = comboBoxColumn.Items.GetEnumerator
    itemIE.Reset()
    Dim thisItem As DataRowView
    Dim exist As Boolean = False
    While itemIE.MoveNext()
        thisItem = CType(itemIE.Current(), DataRowView)
        Dim valueMember As Object = thisItem.Row.ItemArray(0)
        Dim displayMember As Object = thisItem.Row.ItemArray(1)
        If displayMember.ToString = e.FormattedValue Then
            exist = True
        End If
    End While

    If exist = False Then
        If MessageBox.Show(msgAddDesc & e.FormattedValue & "' to the list?", "Does not exist", _
        MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
           comm.CommandText = "INSERT INTO ref_description(desc_description) " & _
               "VALUES(@description)"
            With comm.Parameters
                .Add("@description", MySqlDbType.VarChar).Value = e.FormattedValue
            End With
            comm.ExecuteNonQuery()
            comm.Parameters.Clear()
        End If
    End If
End If
cs2.Close()

现在,每当我向数据库中添加一个新项目时,就会出现此错误,但有时它不会出现:

The following exception occurred in the DataGridView:
System.FormatException: Value '' cannot be converted to type 'System.String'.

at System.Windows.Forms.DataGridViewComboBoxCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)

另一个问题是,如何在将新项目添加到数据库后更新组合框,然后我还想在用户按下回车键或选项卡时自动选择新项目(仅当有新项目添加到数据库中时)数据库)。

2 个答案:

答案 0 :(得分:0)

如何更新组合框:我没有看到代码,如何首先加载ComboBox。

通常,您只需更新相关数据集并将其重新分配给ComboBox.DataSource,或者您可以使用数据绑定(不需要更新,但如果需要更多绑定功能,通常会使用它)。

Provider 'personFactory' must return a value from $get factory method.

...其中cb3是一个ComboBox ......

答案 1 :(得分:0)

我不确定,如果我能提供帮助的话。但是,错误来自DataGridView,而不是组合。我怀疑,您同时更新datagridview(当您尝试更新组合时),这将无法正常工作。我认为你需要先完成单元格编辑(包括验证),然后你可以更新datagridview。我试着把它移到CellEndEdit()事件。