0

我的問題是,我有一個datagridview,我添加來自數據庫的不同產品,每個產品有4個不同的價格存儲在數據庫中。 我從一個ID放在文本框中查看它們,如果ID存在,datagrid將填充它的信息。這裏的一切都很好,問題是我想要將datagridview中每個產品的所有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 

請幫助我,它幾乎做到了我需要重複每一個產品的價格在每行的組合框

謝謝

回答

0

你想嘗試這個例子?這很簡單,我知道,但你可以看到在每個組合在電網使用許多型動物項目:

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 

希望這可以幫助你

+0

謝謝!!現在我可以與這個工作,你給我的想法! –