简体   繁体   中英

datagridview combobox

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM