Results 1 to 4 of 4

Thread: [RESOLVED] Datagridview: move to column 4 but instead to column 5

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Resolved [RESOLVED] Datagridview: move to column 4 but instead to column 5

    I want to move the cursor from column 1 to column 4 but what happened instead the cursor moves to column 5, this happens when I use the enter key event. but run well if I double click.
    The following description of the program:
    there are 7 columns of code, item description, unit, price, qty, discount, item amount
    code when key enter press :
    vb.net Code:
    1. Private Sub dg_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyUp
    2.         If e.KeyData = Keys.Enter Then
    3.             Dim cRow As Integer = dg.CurrentCell.RowIndex
    4.             Dim cCol As Integer = dg.CurrentCell.ColumnIndex
    5.  
    6.             Select Case cCol
    7.                 Case 0, 1, 2
    8.                     LoadProduct()
    9.                     MoveCell(cRow, 4)
    10.         end select
    11.     end if
    12. end sub

    and code when double click at datagridview.
    vb.net Code:
    1. Private Sub dg_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellDoubleClick
    2.         Select Case e.ColumnIndex
    3.             Case 0, 1
    4.                 LoadProduct()
    5.                 MoveCell(cRow, 4)
    6.         End Select
    7. End Sub
    at LoadProduct() method program load form dialog to get product list and then choose one of them.

    vb.net Code:
    1. 'move cursor to crow & ccol cell
    2.  Sub MoveCell(ByVal crow As Integer, ByVal cCol As Integer)
    3.         If crow <= 0 Then crow = 0
    4.         dg.CurrentCell.Selected = False
    5.  
    6.         dg.Rows(crow).Cells(cCol).Selected = True
    7.         dg.CurrentCell = dg.SelectedCells(0)
    8.         dg.Refresh()
    9.  
    10.     End Sub

    I have some ways but still could.

    thank you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Datagridview: move to column 4 but instead to column 5

    Like every collection in .NET, DataGridView.Columns is zero-based. The first column is index 0, the fourth column is index 3.

  3. #3

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Datagridview: move to column 4 but instead to column 5

    agree, the index of the column in the grid starting from 0.
    I want my code to move from column to column qty but what happened instead move the cursor to the column discount

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Datagridview: move to column 4 but instead to column 5

    Can you not just do this?
    vb.net Code:
    1. myDataGridView.CurrentCell = myDataGridView(desiredColumnIndexOrName, e.RowIndex)

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