开发者

DataGridViewComboBoxColumn autocomplete

开发者 https://www.devze.com 2023-02-20 11:39 出处:网络
I have datagridview containing 4 columns with 2 combobox. With the initial loading of datagridview I could select an item with the combobox, but when I tried to select an item with other comb开发者_如

I have datagridview containing 4 columns with 2 combobox. With the initial loading of datagridview I could select an item with the combobox, but when I tried to select an item with other comb开发者_如何学运维obox I've got an error showing "System.ArgumentException: DataGridViewComboBoxCell Value is not valid. To replace this default dialog please handle the dataerror event" . When ever I click it's always shows this message.

Any suggestion would greatly appreciated

Thanks in advance

Tirso

Here is the code

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If DataGridView1.CurrentCell.ColumnIndex = 0 Then
        Dim sSQL As String = "SELECT * FROM pr_employees LEFT OUTER JOIN pr_employees_other_info ON pr_employees.employee_id = pr_employees_other_info.employee_id"
        ReadSQL(sSQL)

        Dim dtTable As New DataTable
        dtTable.Load(reader)

        Dim cbo As ComboBox = CType(e.Control, ComboBox)
        cbo.DropDownStyle = ComboBoxStyle.DropDown
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems

        cbo.DataSource = dtTable
        cbo.DisplayMember = "first_name"
        cbo.ValueMember = "employee_id"
    End If
End Sub


DGV uses only one combobox for all cells and columns.

You need remove all asigned properties / events to combobox before show it on other cell.

Using EditingControlShowing, reset the properies when control is a combobox, then assign it for column = 0.

You can do same behaviour using a custom DataGridviewComboBoxCell (inherits from).

Then you can override InitializeEditingControl and DetachEditingControl.

0

精彩评论

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