2011-07-19 43 views
0

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

任何建议将不胜感激

我尝试下面的代码,但有一个错误说法,但“出现在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 

回答

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 
+0

嗨马丁,感谢您的伟大的解决方案。它像一个魅力。 – tirso