locked
From datagridview Column to combobox RRS feed

  • Question

  • Hello,

    Is it possible to get all items from a datagridview Column ( from 1 to 6 items) to a combobox ?

    I fill the datagridview from a sqlce database.

    How do i get this to work , via datagridview or directly from the database .

    The Column name is Product

    And this is what i am looking for in a combobox

    i fill the datagridview with this.

     cmd = New SqlCeCommand("SELECT Id, Product, Jaar FROM Cijfers WHERE Reisnr ='" & Reisnr.Text & "'", con)
                If con.State = ConnectionState.Closed Then con.Open()
                myDA = New SqlCeDataAdapter(cmd)
                myDataSet = New DataSet()
                myDA.Fill(myDataSet, "MyTable")
                DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
                If con.State = ConnectionState.Open Then con.Close()

    Friday, December 28, 2018 2:52 PM

Answers

  • Hi

    Well, certainly seems a waste to me, but here is some code - amodification of the earlier code that will do what you want (I think)

    ' Form1 with ComboBox1
    ' and Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyTable As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' this is a table just for this
        ' example - you already have your
        ' MyTable set up from your database
        With MyTable
          .Columns.Add("One", GetType(Integer))
          .Columns.Add("Two", GetType(Integer))
          .Columns.Add("Three", GetType(Integer))
          .Columns.Add("Products", GetType(String))
          .Columns.Add("Four", GetType(Integer))
    
          .Rows.Add(11, 12, 13, "Blauw", 14)
          .Rows.Add(21, 22, 23, "Zwart Hout", 24)
          .Rows.Add(31, 32, 33, "Another word I don't understand", 34)
          .Rows.Add(41, 42, 43, "A foreign word", 44)
        End With
        ' ---------------------------------
    
        Dim lst As New List(Of String)
        lst.Add(ConcCB())
    
        With ComboBox1
          .DataSource = lst
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .DropDownStyle = ComboBoxStyle.DropDown
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
        Label1.Text = "Selected value"
        Label2.Text = ConcCB()
      End Sub
    
      Private Sub ComboBox1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Label1.Text = ComboBox1.SelectedValue.ToString
      End Sub
      Function ConcCB() As String
        Dim s As String = Nothing
        For Each dr As DataRow In MyTable.Rows
          s &= dr.Item("Products").ToString & "/"
        Next
        Return s.Substring(0, s.Length - 1)
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Sunday, December 30, 2018 10:03 PM
    Friday, December 28, 2018 7:16 PM

All replies

  • I use this i a other project 

      cmd = New SqlCeCommand("Select Id, Product FROM Cijfers WHERE Product <>''", con)
            If con.State = ConnectionState.Closed Then con.Open()
            Dim sdr As SqlCeDataReader = cmd.ExecuteReader()
            While sdr.Read = True
                Product.Items.Add(sdr.Item("Product"))
            End While
            sdr.Close()

    Then the outcome is 

    I am looking for this


    • Edited by KeesBlunder Friday, December 28, 2018 3:17 PM
    Friday, December 28, 2018 3:16 PM
  • Hi

    Here is some code to illustrate what I think you want, I have chosen an arbitrary set of data just for the example - you already have MyTable from your database.

    This example set up the ComboBox to display the Products but to give a value from Column named 'Four', and, has 'SuggestAppend' included.

    ' Form1 with ComboBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyTable As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' this is a table just for this
        ' example - you already have your
        ' MyTable set up from your database
        With MyTable
          .Columns.Add("One", GetType(Integer))
          .Columns.Add("Two", GetType(Integer))
          .Columns.Add("Three", GetType(Integer))
          .Columns.Add("Products", GetType(String))
          .Columns.Add("Four", GetType(Integer))
    
          .Rows.Add(1, 2, 3, "Blauw", 4)
          .Rows.Add(1, 2, 3, "Zwart Hout", 4)
          .Rows.Add(1, 2, 3, "Another word I don't understand", 4)
          .Rows.Add(1, 2, 3, "A foreign word", 4)
        End With
        ' ---------------------------------
    
    
        With ComboBox1
          .DataSource = MyTable
          .DisplayMember = "Products"
          .ValueMember = "Four"
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .DropDownStyle = ComboBoxStyle.DropDown
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Friday, December 28, 2018 3:18 PM
  • Hi

    For completeness, here is the same code with a tiny addition to show the use of the 'ValueMember' - NOTE: it is used via  ComboBox1.SelectedValue.

    ' Form1 with ComboBox1
    ' and Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyTable As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' this is a table just for this
        ' example - you already have your
        ' MyTable set up from your database
        With MyTable
          .Columns.Add("One", GetType(Integer))
          .Columns.Add("Two", GetType(Integer))
          .Columns.Add("Three", GetType(Integer))
          .Columns.Add("Products", GetType(String))
          .Columns.Add("Four", GetType(Integer))
    
          .Rows.Add(11, 12, 13, "Blauw", 14)
          .Rows.Add(21, 22, 23, "Zwart Hout", 24)
          .Rows.Add(31, 32, 33, "Another word I don't understand", 34)
          .Rows.Add(41, 42, 43, "A foreign word", 44)
        End With
        ' ---------------------------------
    
    
        With ComboBox1
          .DataSource = MyTable
          .DisplayMember = "Products"
          .ValueMember = "Four"
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .DropDownStyle = ComboBoxStyle.DropDown
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
        Label1.Text = "Selected value"
      End Sub
    
      Private Sub ComboBox1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Label1.Text = ComboBox1.SelectedValue.ToString
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, December 28, 2018 3:31 PM
    Friday, December 28, 2018 3:30 PM
  • Hi

    After re-reading, I think maybe you are wanting something very different to my guess.

    You seem to be wanting a concatenation of all the items in a specific column, separated with a "/".

    That is simple to get,but WHY put it in a ComboBox? A ComboBoc is used to select from a list of items where you seem to want only a single line - a Label would serve the same purpose and a lot less cumbersome to use.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, December 28, 2018 3:46 PM
    Friday, December 28, 2018 3:45 PM
  • Hello Les,

    Yes this is what i am looking for separated with a "/"

    I am making a new version of a programm that we are using for a few years.

    And those items where in a combobox using the Collection

    Now they are coming from a database , to keep it uniform i want to put in in a combobox again.

    The reason to seperated them with "/" now i want to show and print all the items in one place in a combobox or textbox .

    Do you have an example to make it show in a label separated with a "/"

    • Edited by KeesBlunder Friday, December 28, 2018 7:06 PM
    Friday, December 28, 2018 7:00 PM
  • Hi

    Well, certainly seems a waste to me, but here is some code - amodification of the earlier code that will do what you want (I think)

    ' Form1 with ComboBox1
    ' and Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyTable As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' this is a table just for this
        ' example - you already have your
        ' MyTable set up from your database
        With MyTable
          .Columns.Add("One", GetType(Integer))
          .Columns.Add("Two", GetType(Integer))
          .Columns.Add("Three", GetType(Integer))
          .Columns.Add("Products", GetType(String))
          .Columns.Add("Four", GetType(Integer))
    
          .Rows.Add(11, 12, 13, "Blauw", 14)
          .Rows.Add(21, 22, 23, "Zwart Hout", 24)
          .Rows.Add(31, 32, 33, "Another word I don't understand", 34)
          .Rows.Add(41, 42, 43, "A foreign word", 44)
        End With
        ' ---------------------------------
    
        Dim lst As New List(Of String)
        lst.Add(ConcCB())
    
        With ComboBox1
          .DataSource = lst
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .DropDownStyle = ComboBoxStyle.DropDown
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
        Label1.Text = "Selected value"
        Label2.Text = ConcCB()
      End Sub
    
      Private Sub ComboBox1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Label1.Text = ComboBox1.SelectedValue.ToString
      End Sub
      Function ConcCB() As String
        Dim s As String = Nothing
        For Each dr As DataRow In MyTable.Rows
          s &= dr.Item("Products").ToString & "/"
        Next
        Return s.Substring(0, s.Length - 1)
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Sunday, December 30, 2018 10:03 PM
    Friday, December 28, 2018 7:16 PM
  • See if the following would be what you are looking for

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dtProducts As DataTable = _dataOperations.Products()
    
        ' take all products and concatenate
        Dim results = String.Join(" / ",
                                  dtProducts.
                                     AsEnumerable().
                                     Select(Function(row) row.Field(Of String)("ProductName")))
    
        DataGridView1.DataSource = dtProducts
        TextBox1.Text = results
    End Sub


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, December 29, 2018 12:32 AM
  • Hello,

    Is it possible to get all items from a datagridview Column ( from 1 to 6 items) to a combobox ?

    I fill the datagridview from a sqlce database.

    How do i get this to work , via datagridview or directly from the database .

    The Column name is Product

    And this is what i am looking for in a combobox

    i fill the datagridview with this.

     cmd = New SqlCeCommand("SELECT Id, Product, Jaar FROM Cijfers WHERE Reisnr ='" & Reisnr.Text & "'", con)
                If con.State = ConnectionState.Closed Then con.Open()
                myDA = New SqlCeDataAdapter(cmd)
                myDataSet = New DataSet()
                myDA.Fill(myDataSet, "MyTable")
                DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
                If con.State = ConnectionState.Open Then con.Close()

    To me this doesn't make any sense what soever. The sole purpose of a combobox is to display multiple items within a dropdown. If you concat all the rows into one string and add that as an item you are misusing the control. I mean, what do you expect to see in the drop down items?  

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Saturday, December 29, 2018 3:29 AM
  • Thanks Les
    Sunday, December 30, 2018 10:03 PM