DataGridViewComboBox具有多行的列

时间:2014-10-10 07:57:12

标签: vb.net

我有一个DataGridview,数据源设置为从数据库表填充的数据表。在DataGridView中,我有一个ComboBox列,该列填充在数据库中的表中。我有一个事件处理程序,它在.SelectedIndexChange的{​​{1}}上触发。

我有两个问题:

  1. 更改ComboBox时,事件会按预期触发;但是,所选值不会显示在ComboBox中。如果值已更改,则不会再次触发。每次更改值时,我都要求更改所选索引。
  2. 更改一个组合框并触发事件后,其他所有行似乎都不会触发事件处理程序。我觉得这很简单,我很遗憾,但我会很感激任何帮助。
  3. 带有DGV代码的表格:

    ComboBox

    带有程序的模块:

    Imports System.Data.SqlClient
    
    Public Class ComponentQForm
    
        Private Sub ComponentQForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FillDataTableComponentForm()
    
            DataGridView1.ColumnHeadersVisible = True
    
            Dim columnHeaderStyle As New DataGridViewCellStyle()
            columnHeaderStyle.BackColor = Color.Beige
            columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
            DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
    
            DataGridView1.DataSource = DataTableComponentForm
            CreateCboColumn()
            DataGridView1.Columns.Add(cbosupplier)
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = 3 Then
                Dim combo As ComboBox = CType(e.Control, ComboBox)
                If (combo IsNot Nothing) Then
                    RemoveHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
                    AddHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
                End If
            End If
        End Sub
    
        Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim combo As ComboBox = CType(sender, ComboBox)
            combo.SelectedIndex = combo.SelectedIndex
            Dim comboVal As Integer = combo.SelectedIndex + 1
            Dim strSQl3 As String = "SELECT tblSuppliers.Company "
            strSQl3 = strSQl3 & "FROM tblSuppliers "
            strSQl3 = strSQl3 & "WHERE tblSuppliers.Id = "
            strSQl3 = strSQl3 & comboVal
            Dim sel1 As New SqlCommand(strSQl3, MyCn)
            Dim comboValue As String
            comboValue = sel1.ExecuteScalar().ToString()
    
            Dim itemNumber As String = DataGridView1.CurrentRow.Cells(0).Value
    
            GetCostETA(comboValueP:=comboValue, itemNumberP:=itemNumber)
            AddCost()
            AddETA()
            AddTotal()
            Populating()
    
        End Sub
    End Class
    

1 个答案:

答案 0 :(得分:0)

可能导致问题的原因太多了。 不幸的是,我不够专业,无法识别问题。也就是说,有些事情引起了我的怀疑:

  1. combo的展示位置和对象的范围。在原始帖子中,代码显示combo对象在本地级别被声明和实例化,例如在方法体内。这意味着一旦离开方法体,combo就不再存在。
  2. 您正在尝试处理对象范围之外的本地范围对象(combo)的事件。此外,事件处理程序不指定事件处理程序正在监听哪个对象,例如, Handles combo.[Event]
  3. 您有一些命名冲突,可能还会导致一些进一步的问题:
  4. 来自原帖:

    Dim combo As ComboBox = CType(sender, ComboBox)
    combo.SelectedIndex = combo.SelectedIndex ' Which combo is receiving 
                                              ' which combo's SelectedIndex?