【问题标题】:vb.net Error after Adding typed text item from datagridViewComboboxColumn into database将 datagridViewComboboxColumn 中的键入文本项添加到数据库后的 vb.net 错误
【发布时间】:2015-12-11 07:28:14
【问题描述】:

基本上,我不希望用户能够输入 datagridViewComboboxColumn 如果没有匹配,它会自动将文本保存到数据库中,更新 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)

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

【问题讨论】:

  • e.FormattedValue改成e.FormattedValue.ToString (.Add("@description", MySqlDbType.VarChar).Value = e.FormattedValue.ToString )试试
  • 我已经尝试过您的建议wingedpanther,但错误仍然存​​在。

标签: vb.net datagridcomboboxcolumn


【解决方案1】:

我不确定,如果我能提供帮助。但是,错误来自 DataGridView,而不是组合。我怀疑,您同时更新了 datagridview(当您尝试更新组合时),这是行不通的。我认为您需要先完成单元格编辑(包括验证),然后才能更新 datagridview。我会尝试将其移至 CellEndEdit() 事件。

【讨论】:

    【解决方案2】:

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

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

    Private Sub LoadUpdateSubListCombo()
            Dim cmdText as String
            cmdText = "SELECT ID,SubName FROM SubmarinesTable "
            Dim ds as DataSet
            ds = MyDataAccessLayer.GetQueryResults(cmdText)
            Dim SubDT As DataTable = ds.Tables(0).DefaultView
            cb3.DataSource = SubDT
            cb3.ValueMember = "ID"
            cb3.DisplayMember = "SubName"
    End Sub
    

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

    【讨论】:

    • 很抱歉我的代码不完整,但我帖子中的第一个代码块是我加载组合框的地方。如果你看得清楚,有一行.DataSource = ds4 是数据所在的位置,而该代码块只是我在表单加载中调用的函数的一部分。我问我在哪里更新组合框源,因为每次我在 CellValidating 事件中执行此操作时都会出现运行时错误。
    • 抱歉,我错过了那个组合设置。我不确定,如果我能帮忙。但是,错误来自 DataGridView,而不是组合。我怀疑,您同时更新了 datagridview(当您尝试更新组合时),这是行不通的。我认为您需要先完成单元格编辑(包括验证),然后才能更新 datagridview。我会尝试将其移至 CellEndEdit() 事件。
    • 非常感谢!我能够使用CellEndEdit() 事件更新并选择组合框中的新项目。显然,我只需要在Try-Catch 中包含我将数据插入数据库的那段代码。我不知道为什么这解决了这个问题,但它确实有效。大声笑现在,如果您可以将评论作为帖子发表,那么我可以接受它作为答案。 TY
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多