I have a text box , Combo box and a dgv in a form . I want to insert text box input and Combo box selected item into dgv. combo box is bound to database table column. can someone provide me with a link or sample code?
Thanks

Please, show your efforts to do your job first, then forum could give you a suggestion to solve.

Thank you for your reply .
I have tried the following code to insert combobox selected item into datagridview . I am getting an error
"Cells' is not a member of 'System.Windows.Forms.DataGridView"

        For Each dgv As DataGridView In DataGridView1.Rows
        If dgv.Cells(0).text <> ComboBox1.SelectedText.ToString() Then
         DataGridView1.Rows.Add(ComboBox1.SelectedText.ToString())
        End If
    Next 

The Cells collection is a property of the Row class which is indexed in the Rows property of the DGV. You have to identify which row and which cell to insert the data.

Your loops should be started bt

        For Each rw As DataGridViewRow In DataGridView1.Rows

I have draged and droped the datagridview in form designer. datagridview1 is the name of datagrid view , after adding the line
For Each rw As DataGridViewRow In DataGridView1.Rows
I am getting the error -
dgv is not declared .it may be inaccessible due to its protection level.

When you change the name in the loop declaration you have to use that name(rw) inside the loop, not the previous one(dgv).

I here post an exmple to avoid duplicate value to insert in DGV. Here I daclare a function which can detect that the value is already inserted or not in DGV.

 Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not ComboBox1.SelectedItem = Nothing Then

            If Not Me.DuplicateValue(ComboBox1.Items(ComboBox1.SelectedIndex)) Then
                DataGridView1.Rows.Add(ComboBox1.Items(ComboBox1.SelectedIndex))
            End If


        End If
    End Sub

    Private Function DuplicateValue(key As String) As Boolean
        Dim Result As Boolean = False

        For Each rw As DataGridViewRow In DataGridView1.Rows
            If rw.Cells(0).Value = key Then
                Result = True
                Exit For
            End If
        Next

        Return Result
    End Function

suppose, it can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.