【发布时间】: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