[RESOLVED] move cursor in selected rows in datagridview
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] move cursor in selected rows in datagridview

  1. #1
    Join Date
    Jul 2006
    Location
    Mnila, Philippines
    Posts
    171

    [RESOLVED] move cursor in selected rows in datagridview

    how can i move the cursor of the datagridview on the selected rows on datagridview.

    i used this code and it highlight the rows which matches to txtSearch but the cursor did not move in the selected rows.

    Code:
    For x As Integer = 0 To Me.Datagrid.Rows.Count - 1
                If UCase(Me.Datagrid.Item(1, x).Value.ToString) = Trim(Me.txtSearch.Text) Then
                    Me.Datagrid.Rows(i).Selected = True
                    Exit Sub
                End If
            Next
    Thanks in advance

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: move cursor in selected rows in datagridview

    Try this :

    Code:
            For x As Integer = 0 To Me.Datagrid.Rows.Count - 1
                If UCase(Me.Datagrid.Item(1, x).Value.ToString) = Trim(Me.txtSearch.Text) Then
                    Me.Datagrid.Rows(i).Selected = True
                    Me.DataGrid.CurrentCell = DataGridView1.Item(1, x)
    
                    Exit Sub
                End If
            Next
    I hope it helps!

  3. #3
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: move cursor in selected rows in datagridview

    Quote Originally Posted by HanneSThEGreaT View Post
    Try this :

    Code:
            For x As Integer = 0 To Me.Datagrid.Rows.Count - 1
                If UCase(Me.Datagrid.Item(1, x).Value.ToString) = Trim(Me.txtSearch.Text) Then
                    Me.Datagrid.Rows(i).Selected = True
                    Me.DataGrid.CurrentCell = DataGridView1.Item(1, x)
    
                    Exit Sub
                End If
            Next
    I hope it helps!
    Me.DataGridView1.CurrentCell = DataGridView1.Item( DataGridView1.FirstDisplayedCell.ColumnIndex , x)

    to avoid Current cell cannot be set to an invisible cell. error. Which is expected when DataGridView1.columns(1).visible=false.
    It is better to check DataGridView1.rows(x).visible along with text search condition
    UCase(Me.Datagridview1.Item(1, x).Value.ToString) = Ucase(Trim(Me.txtSearch.Text)).
    Last edited by ComITSolutions; June 2nd, 2009 at 11:41 PM.
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  4. #4
    Join Date
    Jul 2006
    Location
    Mnila, Philippines
    Posts
    171

    Re: move cursor in selected rows in datagridview

    Thanks guys, It works perfectly... How about when I search for a record that is placed in the bottom of the datagrid, it will automatically move there so that the user will not scroll the datagrid just to find the highlighted row....

    Thanks again

  5. #5
    Join Date
    Jul 2006
    Location
    Mnila, Philippines
    Posts
    171

    Re: move cursor in selected rows in datagridview

    Me.Datagrid.CurrentCell = me.Datagrid.Rows(i).Cells(1)

    it works fine.... thanks

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: move cursor in selected rows in datagridview

    Glad you came right, good work!

    And thanx for sharing!

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center