DataGridViewComboBoxColumn data selection

  • 14 years ago


    If I got a datagridview like this:


    |Column 1 |Column 2 |

    |This column is |This column is |

    |DataGridViewComboBoxColumn |DataGridViewTextBoxColumn |

    The column 1 has the primary key. How can I get corresponding data of the primary key in the column 2?


    Database table:

    |id |name |

    |51 |Alexis Mendez |

    |12 |Viviana Melendez |

    If I select the number 51 in the column 1 in the datagridview the column 2 get the name Alexis Méndez. How can I do that?

    There is no properties configuration for this?

  • 14 years ago
    That's not really the way it is supposed to work.  The idea is that you bind a DataTable to your grid and one column is a foreign key.  You would then make that column a combo box column and bind the parent table to it.  That allows you to select a parent value from the combo box and have the foreign key value of that child row set to the corresponding value.

    For what you're asking you shouldn't be using a grid at all.  You should just have a ComboBox and a TextBox bound to the same source.  Then when you select an ID in the ComboBox the TextBox will be updated automatically.

  • 14 years ago

    I have one datagridview with the next schema
    |Item #    |Description        |Date        |Due Date   |Price       |

    Where the display data corresponds to the transactions table that I have in my database and the item information comes other table called items.

    What I want is that if I select one Item # from the combobox the description and the price of the item displays automatically.

    All the data displayed in the datagridview is going to be store in the transaction table even the description and the price of the item because some transactions not necessary come from items some can be input manually like debts that only need the debt description and the amount.

    Please post code samples.

  • 14 years ago



    I have done this, but the datagridview get the values when the combobox column lost the focus not when the value changes.


    Imports System.Data

    Imports System.Data.Odbc


    Public Class Form1


        Dim cn As OdbcConnection

        Dim ds As DataSet

        Private WithEvents DataGridView1 As New DataGridView()


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim ConnectionString As String = "FILEDSN=C:\Documents and Settings\Alexis\Desktop\Video System\videodb.dsn;"


            cn = New OdbcConnection(ConnectionString)



            Dim da As OdbcDataAdapter = New OdbcDataAdapter("SELECT id, description, new_rel_charge FROM items ORDER BY id; ", cn)

            ds = New DataSet

            da.FillSchema(ds, SchemaType.Mapped, "Items")

            da.Fill(ds, "Items")


            Dim da2 As OdbcDataAdapter = New OdbcDataAdapter("SELECT item_id, description, date_done, due_date, return_date, amount_to_pay FROM rents ORDER BY id; ", cn)

            da2.FillSchema(ds, SchemaType.Mapped, "Rents")

            da2.Fill(ds, "Rents")



            dataGridView1.AutoGenerateColumns = False

            DataGridView1.DataSource = ds.Tables("Rents").DefaultView


            Dim ComboBoxColumn1 As New DataGridViewComboBoxColumn

            With ComboBoxColumn1

                .HeaderText = "ITEM #"

                .DataPropertyName = "item_id"

                .DataSource = ds.Tables("Items").DefaultView

                .DisplayMember = "id"

            End With


            Dim TextBoxColumn1 As New DataGridViewTextBoxColumn

            TextBoxColumn1.HeaderText = "DESCRIPTION/TITLE"

            TextBoxColumn1.Width = 200

            TextBoxColumn1.DataPropertyName = "description"


            Dim TextBoxColumn2 As New DataGridViewTextBoxColumn

            TextBoxColumn2.HeaderText = "DATE"

            TextBoxColumn2.DataPropertyName = "date_done"


            Dim TextBoxColumn3 As New DataGridViewTextBoxColumn

            TextBoxColumn3.HeaderText = "DUE DATE"

            TextBoxColumn3.DataPropertyName = "due_date"


            Dim TextBoxColumn4 As New DataGridViewTextBoxColumn

            TextBoxColumn4.HeaderText = "RETURN DATE"

            TextBoxColumn4.DataPropertyName = "return_date"


            Dim TextBoxColumn5 As New DataGridViewTextBoxColumn

            TextBoxColumn5.HeaderText = "PRICE"

            TextBoxColumn5.DataPropertyName = "amount_to_pay"









            DataGridView1.SetBounds(12, 12, 410, 362)


        End Sub


        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

            If e.ColumnIndex = 0 Then

                Dim dr As DataRow = ds.Tables("Items").Rows.Find(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)

                If Not dr Is Nothing Then

                    DataGridView1.Item(1, e.RowIndex).Value = dr("description")

                    DataGridView1.Item(2, e.RowIndex).Value = Now.Date

                    DataGridView1.Item(5, e.RowIndex).Value = dr("new_rel_charge")

                End If

            End If

        End Sub

    End Class

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”