首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多行DataGridViewComboBox列

多行DataGridViewComboBox列
EN

Stack Overflow用户
提问于 2014-10-10 07:57:12
回答 1查看 718关注 0票数 0

我有一个将数据源设置为从数据库表填充的datatable的DataGridview。在DataGridView中,我有一个从数据库中的表中填充的ComboBox列。我有一个在.SelectedIndexChange of ComboBox上触发的事件处理程序。

我有两个问题:

  1. ComboBox被更改时,事件会按预期的方式触发;但是,所选的值不会在ComboBox中显示。如果值被更改,则不会再次触发。每当值被更改时,我都需要所选的索引进行更改。
  2. 更改一个组合框并触发事件后,其他行似乎都不会触发事件处理程序。我猜这是相当简单的,我错过了一些相当容易的东西,但任何帮助都将受到感谢。

使用DGV代码的表单:

代码语言:javascript
复制
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

有程序的模块:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-10-16 20:20:05

有太多的事情可能是问题的原因。不幸的是,我没有足够的专家能够找出这个问题。话虽如此,以下是一些引起我怀疑的事情:

  1. combo的位置和对象的作用域。在最初的文章中,代码显示combo对象是在本地级别声明和实例化的,例如在方法体中。这意味着一旦离开了方法体,combo就不再存在了。
  2. 您正在尝试处理对象范围之外的本地作用域对象(combo)的事件。此外,事件处理程序没有指定事件处理程序正在侦听的对象,例如Handles combo.[Event]
  3. 您有一些命名冲突,可能还会导致一些进一步的问题:

从原员额:

代码语言:javascript
复制
Dim combo As ComboBox = CType(sender, ComboBox)
combo.SelectedIndex = combo.SelectedIndex ' Which combo is receiving 
                                          ' which combo's SelectedIndex?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26294516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档