我有一个DataGridview
,数据源设置为从数据库表填充的数据表。在DataGridView
中,我有一个ComboBox
列,该列填充在数据库中的表中。我有一个事件处理程序,它在.SelectedIndexChange
的{{1}}上触发。
我有两个问题:
ComboBox
时,事件会按预期触发;但是,所选值不会显示在ComboBox
中。如果值已更改,则不会再次触发。每次更改值时,我都要求更改所选索引。 带有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
答案 0 :(得分:0)
可能导致问题的原因太多了。 不幸的是,我不够专业,无法识别问题。也就是说,有些事情引起了我的怀疑:
combo
的展示位置和对象的范围。在原始帖子中,代码显示combo
对象在本地级别被声明和实例化,例如在方法体内。这意味着一旦离开方法体,combo
就不再存在。 combo
)的事件。此外,事件处理程序不指定事件处理程序正在监听哪个对象,例如, Handles combo.[Event]
来自原帖:
Dim combo As ComboBox = CType(sender, ComboBox)
combo.SelectedIndex = combo.SelectedIndex ' Which combo is receiving
' which combo's SelectedIndex?