Results 1 to 6 of 6

Thread: [RESOLVED] DataGridview.currentCell()

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Resolved [RESOLVED] DataGridview.currentCell()

    Hi,
    i have a dataGridView and I'm trying to automatically move focus to the second cell on the current row when the first column receives focus. i tried these 2 methods below and both returning errors

    Code:
    Private Sub grdparameters_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdparameters.CellEnter
    
            If e.ColumnIndex = 0 Then
                grdparameters.CurrentCell = grdparameters(1, e.RowIndex)
            End If
        End Sub
    
        Private Sub grdparameters_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdparameters.CurrentCellChanged
            MsgBox(grdparameters.CurrentCell.ColumnIndex)
            If grdparameters.CurrentCell.ColumnIndex = 0 Then
                grdparameters.CurrentCell = grdparameters(1, grdparameters.CurrentRow.Index)
            End If
        End Sub
    What am i missing here ?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridview.currentCell()

    Don't know what you're missing but we're missing the error messages that might give us a bit of a clue!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: DataGridview.currentCell()

    1st error:
    Operation is not valid because it results in a reentrant call to the setCurrentAddressCore
    2nd error:
    object ref not set to an instance of an object

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: DataGridview.currentCell()

    try the _CellClick event:

    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = 0 Then
            DataGridView1.CurrentCell = DataGridView1(1, e.RowIndex)
        End If
    End Sub

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: DataGridview.currentCell()

    here's a better solution for you:

    Code:
    Public Class Form1
    
        Delegate Sub SetColumnIndex()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            grdparameters.Rows.Add()
        End Sub
    
        Private Sub grdparameters_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdparameters.CellEnter
            If e.ColumnIndex = 0 Then
                Dim method As New SetColumnIndex(AddressOf Mymethod)
                grdparameters.BeginInvoke(method)
            End If
        End Sub
    
        Private Sub Mymethod()
            grdparameters.CurrentCell = grdparameters.CurrentRow.Cells(1)
            'grdparameters.BeginEdit(True)
        End Sub
    
    End Class

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: DataGridview.currentCell()

    Thanks .Paul,, this worked

Posting Permissions

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



Click Here to Expand Forum to Full Width