datagridview combobox

时间:2011-07-19 06:36:57

标签: vb.net

我有一个包含第一列(组合框)的datagridview,第二列和第三列是文本框。使用数据表填充组合框。我的问题是在加载表单时,我将从我的数据库中获取记录,并根据这些记录设置我的组合框的值。因此,如果我的数据库中有5条记录,那么在我的datagridview中我应该有5行包含组合框。

任何建议都会非常感谢

我尝试了下面的代码但是有一个错误说“datagridview中发生了以下异常....”但是它会正确显示,但如果我点击任何单元格,那么总是会出现错误。

Private Sub frmEditIngredientManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    sSQL = "SELECT * FROM fs_nutrient"
    ReadSQL(sSQL)

    Dim dtNutrient As New DataTable
    dtNutrient.Load(reader)

    dgvCbxIngredientList.DataSource = dtNutrient
    dgvCbxIngredientList.DisplayMember = "ndb_no"
    dgvCbxIngredientList.ValueMember = "nutrient_id"

    sSQL = "SELECT * FROM fs_ingredient_management_nutrient INNER JOIN fs_nutrient ON fs_ingredient_management_nutrient.nutrient_id = fs_nutrient.nutrient_id WHERE ingredient_management_id = " & intIngredientManagementId & " "
    ReadSQL(sSQL)

    If reader.HasRows Then
        While reader.Read
            Dim row As String() = New String() {reader("ndb_no"), "dd", "vv"}
            dgvNutrient.Rows.Add(row)
        End While
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

全新答案。找到解决错误的方法:

MSDN datagridviewcomboboxcolumn helpfile

中找到了答案

将此例程添加到“报告错误”并忘记您收到的错误:

Private Sub dgvNutrient_DataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs) Handles dgvNutrient.DataError
    'MessageBox.Show("Error happened " & e.Context.ToString())
End Sub