dcsimg
Results 1 to 4 of 4

Thread: Search and Select the first row in Datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    39

    Search and Select the first row in Datagridview

    Hi,

    i have wrote the code to search the field in the datagridview. Now i want to select that first row datagridview of the search result automatically, can anyone help me out to resolve this. Below is my code for search.

    Code:
        Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
            If Not cnn.State = ConnectionState.Open Then
                'open connection
                cnn.Open()
            End If
            Dim searchText = txtsearch.Text
            If searchText = String.Empty Then
                MessageBox.Show("Please enter search text.")
            Else
                Dim sSql As String
                sSql = "Select * From [" & ComboBox1.Text & " ]"
                sSql &= " Where " & " [" & ComboBox2.Text & " ]"
                sSql &= " Like '" & txtsearch.Text & "%'"
                da.SelectCommand = New OleDbCommand(sSql, cnn)
                cmdbldr = New OleDbCommandBuilder(da)
                Dim dt As New DataTable   
                da.Fill(dt) 
                Me.DataGridView1.DataSource = dt
                    End If
    
    cnn.close()
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,752

    Re: Search and Select the first row in Datagridview

    Um, no you didn't. That code is not searching a DataGridView. It is searching a database and displaying the results in a DataGridView. They are quite different things.

    Regardless, selecting a row in a DataGridView is a matter of setting the Selected property of the row to True. Note that selecting a row and setting the current row are two different things. There is always one and only one current row while there may be zero, one or more selected rows.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    39

    Re: Search and Select the first row in Datagridview

    Quote Originally Posted by jmcilhinney View Post
    Um, no you didn't. That code is not searching a DataGridView. It is searching a database and displaying the results in a DataGridView. They are quite different things.

    Regardless, selecting a row in a DataGridView is a matter of setting the Selected property of the row to True. Note that selecting a row and setting the current row are two different things. There is always one and only one current row while there may be zero, one or more selected rows.
    Thank You jmcilhinney, How can i do that, because i having n No. of search results and but i want to select the first row of the result. if click the next button it need move on the next result.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,752

    Re: Search and Select the first row in Datagridview

    What you almost certainly ought to be doing is using a BindingSource and, possibly, a BindingNavigator. If you bind your DataTable to a BindingSource and bind that to the grid then you can call MoveNext and MovePrevious to navigate the current row. If you associate a BindingNavigator with the BindingSource then it provides a UI that will automatically call those methods.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width