hi all
I have an exercise
when I select the value in the combobox will confirm the value on datagridview
I wrote a code in combobox of from1

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         Dim sQry As String
        sQry = "SELECT Name FROM dtaStudent"
        Dim con As SqlConnection = New SqlConnection(sqldb.ConnectionString)
        Dim da As SqlDataAdapter = New SqlDataAdapter(sQry, con)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        ComboBox1.DisplayMember = "Name"
        ComboBox1.ValueMember = "ID"
        ComboBox1.DataSource = dt
        ComboBox1.Focus()
    End Sub
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'code
        form2.ShowDialog()
    End Sub

http://s27.postimg.org/4blpooo8j/Untitled.jpg

I wrote a code in datagridview of from2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sqlQuery As String = "SELECT ID,Name from dtaStudent "
Dim conn As SqlConnection = New SqlConnection(sqldb.ConnectionString)
conn.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(sqlQuery, conn)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub

http://s26.postimg.org/sc7097pcp/Untitled1.jpg

put value on combobox is name
when selected value
if datagridview cell is empty it will error
I want to select the ID instead of Name
because ID is always fixed
but do not know how
expect people to help

When you add the row, you should be able to just access the cell which is the ComboBoxCell and set the value property. Here's a quick example which assumes that column 0 is the combo box:

'First, get the cell as a useful object
'You'll need to know the index of the new row
Dim CBox as DataGridViewComboBoxCell = CType(DGV.Rows(NewRowIndex).Cell(0), DataGridViewComboBoxCell)
Dim CCol as DataGridViewComboBoxColumn = CType(DGV.Columns(0)), DataGridViewComboBoxColumn)

CBox.Value = CCol.Items(CCol.Items.Count - 1)
This article has been dead for over six months. Start a new discussion instead.