0

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

3
Contributors
7
Replies
28
Views
1 Year
Discussion Span
Last Post by Shark_1
0

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

0

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 
0

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.

Edited by tinstaafl

0

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.

0

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

Edited by tinstaafl

0

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.

Edited by Shark_1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.