Results 1 to 7 of 7

Thread: [RESOLVED] Add a combobox column to DGV

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Resolved [RESOLVED] Add a combobox column to DGV

    Here is my code
    Code:
           Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Documenti\Visual Studio 2017\Projects\Rubrica.mdb"
    
            Dim Adapter1 As OleDbDataAdapter
            Dim sSQL As String = "SELECT VOTO FROM VOTI"
            Dim Dt1 As DataTable = New DataTable
            Dim Cmd As New OleDbCommand(sSQL, Conn)
            Adapter1 = New OleDbDataAdapter(Cmd)
            Adapter1.Fill(Dt1)
            DataGridView2.DataSource = Dt1
            Dim column As New DataGridViewComboBoxColumn
    
            DataGridView2.Columns.Add(column)
            column.DataSource = Dt1
    My output is shown on attached image, how can I fix it ?Name:  Cattura.JPG
Views: 203
Size:  18.0 KB

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Add a combobox column to DGV

    Generally, you wouldn't be binding the same datasource both for the dgv and the column...


    Code:
    column.DisplayMember = "field name"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: Add a combobox column to DGV

    it's just to show that with the same datatable I can't populate the combo column.
    Thank You Paul, your suggestion works, why displaymember is needed?
    Last edited by patel45; Apr 9th, 2020 at 10:05 AM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Add a combobox column to DGV

    A datatable is a collection of fields and rows. If you don't specify which field to use, it attempts to use the whole row, which is System.Data.DataRowView

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: [RESOLVED] Add a combobox column to DGV

    My data table has only one field but I understand now, thanks.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Add a combobox column to DGV

    You really need to set the DisplayMember and the ValueMember. Here's how it works. You bind data to your grid as normal so each cell in that column has a Value from that data source. For each cell, the Value is used as a lookup in the column or property of the column data source specified in the ValueMember. That selectes the row from the column data source and the value in the column or property of the column data source specified in the DisplayMember is displayed in the cell. When you edit a cell, a ComboBox control is created and embedded in the cell. The Value of the cell is assigned to the SelectedValue of the editing control and the corresponding item is selected and displayed. When you select an item and end the editing session, the SelectedValue of the ComboBox control is assigned back to the Value of the cell.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: [RESOLVED] Add a combobox column to DGV

    Thanks jmc for the explanation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width