none
Comboboxcolumn in VB.net RRS feed

  • Question

  • I am a new in vb.net and i have an issue , i cant find the way to populate a datagridview columns based on comboboxcolumn

    Example : i have 06 columns one of them is comboboxcolumn i choose the item from combobox and the other columns fill automatically based on the selected item from combobox .

    the data come from SQL server connection from one table 

    N°       Designation                  Price             Qty                  TVA            Amount 

    1     Farine 25 Kg         500.00        05             19%       ---------



    • Edited by salimzz28 Wednesday, August 7, 2019 2:26 PM
    Wednesday, August 7, 2019 1:52 PM

Answers

  • Hi,
    try this demo; DataSet with Relation between Master (Product) and Child (Order); Expression in DataGridViewTextBoxColumn:

    Public Class Form1
    
      Private dgv As New DataGridView With {.Dock = DockStyle.Fill, .AutoGenerateColumns = False}
      Private ds As New DataSet
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(dgv)
        Me.ds = LoadData()
        dgv.DataSource = ds.Tables(1).DefaultView
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "OrderName", .DataPropertyName = "OrderName"})
        dgv.Columns.Add(New DataGridViewComboBoxColumn() With {.HeaderText = "ProductName", .DataSource = ds.Tables(0).DefaultView,
                        .ValueMember = "ID", .DisplayMember = "ProductName", .DataPropertyName = "FK"})
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Price", .DataPropertyName = "Price"})
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Quantity", .DataPropertyName = "Qty"})
      End Sub
    
      Private Function LoadData() As DataSet
        '
        Dim ds As New DataSet
        ' simulate data loading
        Dim rnd As New Random
        ' Master
        Dim dtMaster As New DataTable("Master")
        ds.Tables.Add(dtMaster)
        With dtMaster
          With .Columns
            With .Add("ID", GetType(Integer))
              .AutoIncrement = True
              .AutoIncrementSeed = -1
              .AutoIncrementStep = -1
            End With
            .Add("ProductName", GetType(String))
            .Add("Price", GetType(Decimal))
            .Add("Qty", GetType(Integer))
          End With
          For i = 1 To 10
            .Rows.Add(Nothing, $"Pooduct {i}", rnd.Next(5, 200) / 100, rnd.Next(1, 10))
          Next
        End With
        ' Child
        Dim dtChild As New DataTable("Child")
        ds.Tables.Add(dtChild)
        With dtChild
          With .Columns
            With .Add("ID", GetType(Integer))
              .AutoIncrement = True
              .AutoIncrementSeed = -1
              .AutoIncrementStep = -1
            End With
            .Add("FK", GetType(Integer))
            ds.Relations.Add("Rel", dtMaster.Columns("ID"), dtChild.Columns("FK"))
            .Add("OrderName", GetType(String))
            .Add("Price", GetType(Decimal), "Parent(Rel).Price")
            .Add("Qty", GetType(Integer), "Parent(Rel).Qty")
          End With
          For i = 1 To 100
            .Rows.Add(Nothing, -rnd.Next(1, 11), $"Order {i}")
          Next
        End With
        '
        Return ds
      End Function
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by salimzz28 Tuesday, August 20, 2019 11:36 AM
    Monday, August 19, 2019 2:00 PM

All replies

  • Hello,

    Where does the data to populate the DataGridView come from? How are you accessing the data. Please be specific so that we can moving forward rather than ask questions like those above.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 7, 2019 1:54 PM
    Moderator
  • my question is : I have a Datagridview with Comboboxcolumn for the products and when i select the product the price and unit and Qty (1) of this product will show automatically in Datagridview ( VB.NET)
    Monday, August 19, 2019 11:03 AM
  • Hi,
    try this demo; DataSet with Relation between Master (Product) and Child (Order); Expression in DataGridViewTextBoxColumn:

    Public Class Form1
    
      Private dgv As New DataGridView With {.Dock = DockStyle.Fill, .AutoGenerateColumns = False}
      Private ds As New DataSet
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(dgv)
        Me.ds = LoadData()
        dgv.DataSource = ds.Tables(1).DefaultView
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "OrderName", .DataPropertyName = "OrderName"})
        dgv.Columns.Add(New DataGridViewComboBoxColumn() With {.HeaderText = "ProductName", .DataSource = ds.Tables(0).DefaultView,
                        .ValueMember = "ID", .DisplayMember = "ProductName", .DataPropertyName = "FK"})
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Price", .DataPropertyName = "Price"})
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Quantity", .DataPropertyName = "Qty"})
      End Sub
    
      Private Function LoadData() As DataSet
        '
        Dim ds As New DataSet
        ' simulate data loading
        Dim rnd As New Random
        ' Master
        Dim dtMaster As New DataTable("Master")
        ds.Tables.Add(dtMaster)
        With dtMaster
          With .Columns
            With .Add("ID", GetType(Integer))
              .AutoIncrement = True
              .AutoIncrementSeed = -1
              .AutoIncrementStep = -1
            End With
            .Add("ProductName", GetType(String))
            .Add("Price", GetType(Decimal))
            .Add("Qty", GetType(Integer))
          End With
          For i = 1 To 10
            .Rows.Add(Nothing, $"Pooduct {i}", rnd.Next(5, 200) / 100, rnd.Next(1, 10))
          Next
        End With
        ' Child
        Dim dtChild As New DataTable("Child")
        ds.Tables.Add(dtChild)
        With dtChild
          With .Columns
            With .Add("ID", GetType(Integer))
              .AutoIncrement = True
              .AutoIncrementSeed = -1
              .AutoIncrementStep = -1
            End With
            .Add("FK", GetType(Integer))
            ds.Relations.Add("Rel", dtMaster.Columns("ID"), dtChild.Columns("FK"))
            .Add("OrderName", GetType(String))
            .Add("Price", GetType(Decimal), "Parent(Rel).Price")
            .Add("Qty", GetType(Integer), "Parent(Rel).Qty")
          End With
          For i = 1 To 100
            .Rows.Add(Nothing, -rnd.Next(1, 11), $"Order {i}")
          Next
        End With
        '
        Return ds
      End Function
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by salimzz28 Tuesday, August 20, 2019 11:36 AM
    Monday, August 19, 2019 2:00 PM
  • Thank you Brother this is exactly where I want God Bless you
    Tuesday, August 20, 2019 11:22 AM