问题描述
我正在一个项目中,我有一个datagridview,其数据源设置为从数据库表填充的数据表.在datagridview中,我有一个combobox列,它是从数据库中的一个表填充的.我有一个触发的事件处理程序 组合框的selectedindexchange.我有2个问题.首先,当更改组合框时,事件将按预期触发,尽管所选值未显示在组合框中,并且如果更改了该值,则不会再次触发,我要求它进行更改 每次更改值.其次,在更改了一个组合框并触发了事件之后,其他所有行似乎都没有触发事件处理程序.我怀疑这很简单,我很容易错过一些东西,但是任何帮助都将不胜感激.
I am working on a project where I have a datagridview with the datasource set to a datatable filled from database tables. In the datagridview I have a combobox column, which is populated from a table in the database. I have an event handler which fires on selectedindexchange of the combobox. I am having 2 problems. Firstly when the combobox is changed the event fires as intended although the selected value does not display in the combobox and if the value is changed it does not fire again, I require it to change everytime the value is changed. Secondly, after changing one combobox and the event is fired, none of the other rows seem to fire the event handler. I amguessing it is fairly simple and I am missing something quite easy but any help will be appreciated.
带有datagridview的表单代码:
Code of form with datagridview:
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
带有过程的模块代码:
Imports System.Data.SqlClient
Module ComponentQuoteProcedure
Public DataTableComponentForm As New DataTable
Public DataTableComponentForm2 As New DataTable
Public DataAdpCompForm As New SqlDataAdapter
Public comboValue As String
Public cbosupplier As New DataGridViewComboBoxColumn()
Public Sub FillDataTableComponentForm()
OpenDBCon()
OpenConSuppliers()
Dim strSQl As String
strSQl = "SELECT tbl200AV.ItemNumber, " & _
"tblComponents.PartNumber, " & _
"tbl200AV.Quantity " & _
"FROM tbl200AV, tblComponents " & _
"WHERE tbl200AV.ItemNumber = tblComponents.ItemNumber"
Dim sel As New SqlCommand(strSQl, MyCn)
DataAdpCompForm.SelectCommand = sel
DataAdpCompForm.Fill(DataTableComponentForm)
DataAdpCompForm.Dispose()
End Sub
Public Sub CreateCboColumn()
Dim itemscount As Integer = MyDataTblSuppliers.Rows.Count - 1
cbosupplier.HeaderText = "Supplier"
cbosupplier.Name = "Supplier"
cbosupplier.MaxDropDownItems = itemscount
cbosupplier.DataSource = MyDataTblSuppliers
cbosupplier.DisplayMember = "Company"
End Sub
Public Sub GetCostETA(ByVal comboValueP As String, ByVal itemNumberP As Integer)
Dim strSQl2 As String = "SELECT tblCompSup.ItemNumber, "
strSQl2 = strSQl2 & "tblCompSup.CostPrice, "
strSQl2 = strSQl2 & "tblCompSup.ETA "
strSQl2 = strSQl2 & "FROM tblCompSup, tbl200AV "
strSQl2 = strSQl2 & "WHERE '"
strSQl2 = strSQl2 & itemNumberP
strSQl2 = strSQl2 & "' = tblCompSup.ItemNumber "
strSQl2 = strSQl2 & "AND tblCompSup.Supplier LIKE '"
strSQl2 = strSQl2 & comboValueP
strSQl2 = strSQl2 & "'"
Dim sel As New SqlCommand(strSQl2, MyCn)
DataAdpCompForm.SelectCommand = sel
DataAdpCompForm.Fill(DataTableComponentForm2)
End Sub
Public Sub AddCost()
If Not DataTableComponentForm.Columns.Contains("CostPrice") Then
DataTableComponentForm.Columns.Add("CostPrice")
End If
End Sub
Public Sub AddETA()
If Not DataTableComponentForm.Columns.Contains("ETA") Then
DataTableComponentForm.Columns.Add("ETA")
End If
End Sub
Public Sub AddTotal()
If Not DataTableComponentForm.Columns.Contains("TotalPrice") Then
DataTableComponentForm.Columns.Add("TotalPrice")
End If
End Sub
Public Sub Populating()
Dim newRow() As Data.DataRow
Dim itemNumber As Integer = DataTableComponentForm2.Rows(0).Item("ItemNumber")
newRow = DataTableComponentForm.Select("ItemNumber = '" & itemNumber & "'")
Dim cost As Decimal = DataTableComponentForm2.Rows(0).Item("CostPrice")
Dim eta As Integer = DataTableComponentForm2.Rows(0).Item("ETA")
Dim quant As Integer = DataTableComponentForm.Rows(0).Item("Quantity")
Dim total As Decimal = quant * cost
newRow(0)("CostPrice") = cost
newRow(0)("ETA") = eta
newRow(0)("TotalPrice") = total
End Sub
End Module
推荐答案
>>首先,当更改组合框时,事件将按预期触发,尽管所选值未显示在组合框中,并且如果更改了该值,则不会再次触发,我要求每次更改值时都进行更改.
>> Firstly when the combobox is changed the event fires as intended although the selected value does not display in the combobox and if the value is changed it does not fire again, I require it to change everytime the value is changed.
>>其次,在更改一个组合框并触发事件后,似乎没有其他行触发事件处理程序.
>>Secondly, after changing one combobox and the event is fired, none of the other rows seem to fire the event handler.
我创建了一个非常简单的示例来重现此问题,该示例是否与您列出的问题相同?
I have created a quite simple sample to reproduce this issue, whether this sample has the same issue as you listed?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim test As Object = CType(Me.OwnedForms, Object)
Dim dt As New DataTable
With dt
.Columns.Add("Column1")
.Columns.Add("Column2")
With .Rows
.Add({"1", 1})
.Add({"2", 2})
.Add({"3", 3})
End With
End With
Dim cl1 As New DataGridViewComboBoxColumn
With cl1
.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
.Items.AddRange({"1", "2", "3", "4"})
.DataPropertyName = "Column1"
.HeaderText = "Column1"
.Name = "Column1"
End With
With DataGridView1
.Columns.Add(cl1)
.DataSource = dt
.AllowUserToAddRows = False
.AutoGenerateColumns = False
End With
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 0 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)
Me.Text &= combo.SelectedIndex
End Sub
致谢.
卡尔
这篇关于具有多个行的DataGridViewComboBox列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!