开发者

datagridview combobox

开发者 https://www.devze.com 2023-03-21 20:03 出处:网络
I have a datagridview containing 1st column (combobox), 2nd and 3rd column is textbox. The combobox was filled-up using datatable. My problem is on loading form, I will get a records from my database

I have a datagridview containing 1st column (combobox), 2nd and 3rd column is textbox. The combobox was filled-up using datatable. My problem is on loading form, I will get a records from my database and set the value of my combobox base on those records. So if I have 5 records from my database then I should have 5 rows containing combobox in my datagridview.

Any suggestion would greatly appreciated

I tried the code below but there's an error saying "the following exception occured in the datagridview...." but it will display correctly, but if I click in any cell that error always appear.

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
    dgvCbxIngredie开发者_JAVA技巧ntList.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


Completely new answer. Found a way around the error:

Answer was found in MSDN datagridviewcomboboxcolumn helpfile

Add this routine to "report errors" and forget about the error you get:

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

精彩评论

暂无评论...
验证码 换一张
取 消