【问题标题】:put combobox filled from database into a datagridview column将从数据库填充的组合框放入 datagridview 列
【发布时间】:2015-10-01 19:36:15
【问题描述】:

我的问题是我有一个 datagridview,我在其中添加了来自数据库的不同产品,每个产品在数据库中存储了 4 个不同的价格。 如果 ID 存在,我会从我放入文本框中的 ID 中查看它们,数据网格会填充其信息。这里的一切都很好,问题是我想将每种产品的所有 4 个价格收集在 datagridview 内的一个组合框中。我已经尝试了很多,但没有任何效果。我只能这样做:

    '*****With this I fill a combobox*****
    Dim CBdepartamento As New ComboBox
    Dim Dt1 As DataTable
    Dim Da1 As New SqlDataAdapter
    Dim Cmd1 As New SqlCommand
    'Dim dat As New DataGridViewComboBoxColumn

    With Cmd1
        .CommandType = CommandType.Text
        .CommandText = "select precioventa from productos where idproducto =" & txtcodigo.Text + " UNION select pventa1 from productos where idproducto =" & txtcodigo.Text + " UNION select pventa2 from productos where idproducto =" & txtcodigo.Text + " UNION select pventa3 from productos where idproducto =" & txtcodigo.Text + ""
        .Connection = cn
    End With
    Da1.SelectCommand = Cmd1
    Dt1 = New DataTable
    Da1.Fill(Dt1)
    With CBdepartamento
        .DataSource = Dt1
        .DisplayMember = "precioventa"
        .ValueMember = "precioventa"
    End With
    '*************************************
    '****with this I fill the datagridview with the data obtained of the database***
            Try
        Dim dt As New DataTable


        Using adaptador As New SqlDataAdapter("SELECT idproducto, nombre, precioventa FROM productos WHERE idproducto =" & txtcodigo.Text, cn)
            adaptador.Fill(dt)
        End Using
        dt.Columns.Add("cantidad")
        Dim cantt As Integer = 1
        For Each dr As DataRow In dt.Rows

            dr("cantidad") = cantt
            DataGridView1.Rows.Add(dr.ItemArray)
        Next
        '** this "for" add the pricelist in the cbox in the column "cantidad2" with only one product is fine but if I add another, in each cbox load the four prices of the first product plus the four prices of the second one that is 8 prices in each cbox... 3 products are 12 prices in the list
        For i = 0 To Dt1.Rows.Count - 1
            cantidad2.Items.Add(Dt1.Rows(i).Item("precioventa"))
        Next

请帮帮我,它快完成了,我只需要它在每行的组合框中重复每个产品的价格

谢谢你

【问题讨论】:

    标签: vb.net sql-server-2008 visual-studio-2013 datagridview combobox


    【解决方案1】:

    你想试试这个例子吗?我知道这很简单,但是您可以看到网格中每个组合中使用了多少不同的项目:

    Public Class Form1
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btTest.Click
        Dim dgvColumnText As New DataGridViewTextBoxColumn
        Dim dgvColumnCombo As New DataGridViewComboBoxColumn
    
        dgvColumnText.Name = "Producto"
        dgvColumnText.HeaderText = "Producto"
        myGrid.Columns.Add(dgvColumnText)
        dgvColumnCombo.Name = "Precio"
        dgvColumnCombo.HeaderText = "Precio"
        myGrid.Columns.Add(dgvColumnCombo)
    
        myGrid.Rows.Add(10)
    
        For i As Integer = 0 To myGrid.Rows.Count - 1
            myGrid.Rows(i).Cells("Producto").Value = String.Format("Producto {0}", i)
            Dim c As DataGridViewComboBoxCell
            c = CType(myGrid.Item(1, i), DataGridViewComboBoxCell)
            c.Items.Add(String.Format("Precio {0}", i))
            c.Items.Add(String.Format("Precio {0}", CStr(i * 2)))
        Next
    End Sub
    

    您只需要在表单中添加:DataGridView 控件(代码中的 myGrid)和一个按钮(btTest)。

    如您所见,如果您想为每个单元格填充不同的值,则需要使用 DataGridViewComboBoxCell 控件。

    您会发现另一个问题:当您单击单元格设置价格时,您需要单击两次。如果您不想这样做,则必须使用此代码:

    Private Sub myGrid_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles myGrid.CellClick
        Dim validRow As Boolean = (e.RowIndex <> -1)
        Dim mygrid As DataGridView = CType(sender, DataGridView)
    
        If TypeOf (mygrid.Columns(e.ColumnIndex)) Is DataGridViewComboBoxColumn AndAlso validRow = True Then
            mygrid.BeginEdit(True)
            Dim c As ComboBox = CType(mygrid.EditingControl, ComboBox)
            c.DroppedDown = True
        End If
    End Sub
    

    希望对你有帮助

    【讨论】:

    • 谢谢!!既然你给了我这个想法,我就可以处理这个了!
    猜你喜欢
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多