0

我想知道如何在 vb.net 中使用 DataGridViewComboBoxEditingControl

当用户从我配置的 datagridviewcomboboxcolumn 中选择一个项目时,我需要一个 routteen 来运行。我不确定如何将对象附加到我手动创建的列

我已经从互联网上的示例中实现了以下内容,但这似乎仅在用户单击列中的组合框时触发。

Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
    Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)

    If editingComboBox IsNot Nothing Then
        AddHandler editingComboBox.SelectedValueChanged, AddressOf EditingComboBox_DropDown
    End If
End Sub

Private Sub EditingComboBox_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Debug.WriteLine("A ComboBox in the DataGridView just dropped down.")
End Sub

任何帮助将不胜感激,因为我似乎找不到很多参考资料

提前致谢

4

1 回答 1

1

刚刚意识到我从未发布过我的问题的答案

将以下内容放在 EditingControlShowing 中以捕获请求

Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
    Try
        If dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
            Dim cmbox As ComboBox = TryCast(e.Control, ComboBox)
            AddHandler cmbox.SelectionChangeCommitted, AddressOf Update_StockInfo
            strSelectedText = cmbox.SelectedText
        End If
    Catch ex As Exception

    End Try

End Sub

然后这将项目添加到组合框单元格

Private Sub Update_StockInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim cmbClickedCell = DirectCast(sender, DataGridViewComboBoxEditingControl)
    Dim cmbComboBox = DirectCast(sender, ComboBox)



    If dgvTicketDetail.CurrentRow.Index = cmbClickedCell.EditingControlRowIndex And dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
        Debug.WriteLine(cmbClickedCell.EditingControlRowIndex & cmbComboBox.SelectedValue)



        Dim dtStock As DataTable = CropTrackMod.GetWeight(cmbComboBox.SelectedValue)

        Dim dgvcc As DataGridViewComboBoxCell
        dgvcc = dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells(2)
        dgvcc.Items.Clear()

        For Each row As DataRow In dtStock.Rows
            dgvcc.Items.Add(row.Item("UnitName"))
        Next row

        If CropTrackMod.IsStockVATAble(cmbComboBox.SelectedValue) = True Then
            dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = CropTrackMod.dblVATRate
        Else
            dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = "0.00"
        End If



    End If
End Sub

它对我来说真的很好用,不幸的是用户最终想要一些不同的东西哈。哦,至少我学到了一些东西

于 2014-01-21T21:03:02.727 回答