绑定后向datagridview组合框添加键/值

时间:2017-01-31 06:45:22

标签: c# vb.net datagridview combobox

我在处理有关DataGridView ComboBoxes的错误时遇到问题。

理想情况下,如果在加载我的DataGridView Combobox列并找不到相应的键时,我想使用.ErrorText显示该键以及错误消息,并且不会将所述键显示为Combobox中的选项。

这个问题是由于存在不同的"类别而产生的。对于每个对象,每个类别都有一组可用于DataGridView Combobox列的值。当用户更改对象所属的类别时,在我的DataGridView组合框中选择的选项可能是也可能不是该类别的一部分。整个列将具有相同的选项。保存时会进行验证,因此无效"无效"网格中的条目不是问题,但我想引导用户只选择有效的选项。

目前我正在填充我的Combobox专栏:

colTest.Populate("myTable", "ID", "Value", "WHERE categoryID = 1")

<Extension>
Public Sub Populate(ByRef cbo As DatagridviewComboboxColumn, table As String, key As String, value As String, Optional where As String = "")
    Dim mySQLAccess As CustomSQLAccess 'Using Dll to interact with SQL'
    Dim myTmpTable As DataTable
    Dim comboBox = New Dictionary(Of Integer, String)

    Try
        myTmpTable = mySQLAccess.ExecuteSelectQuery("SELECT " + key + ", " + value + " FROM " + table + " " + where)

        For Each row As DataRow In myTmpTable.Rows
            comboBox.Add(row.Item(0), row.Item(1))
        Next

        If comboBox IsNot Nothing Then
            cbo.DataSource = New BindingSource(comboBox, Nothing)
            cbo.DisplayMember = "Value"
            cbo.ValueMember = "Key"
        End If

    Catch ex As Exception
        'Error handling'
    End Try
End Sub

我想设置错误消息并添加&#34;无效&#34; .DataError事件的条目但不确定如何编码。

Private Sub myGrid_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles myGrid.DataError

    Dim view2 = CType(sender, DataGridView)
    view2.Rows(e.RowIndex).ErrorText = "an error"
    view2.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = "an error"

End Sub

如果在Combobox中没有选项的情况下无法显示该键,是否可以将(键,&#34;键&#34;)添加到组合框中?

0 个答案:

没有答案