Move to cell in previous row?

I need to write a code to move to a cell in the previous row.

I have looked at (offset) , (currentcell) & (currentcelladdress) but i can't quite get the hang of it.

This is what i have so far in a button click event:

     Me.MaintableDataGridView.CurrentCell.Selected = False

     Me.MaintableDataGridView.Rows(0).Cells(0).Selected = True

This works great but i just want to move 1 cell up, not by coordinates.

Is there some way to utilize code to mimic the arrow keys (sendkeys, etc...)

Any help would be great.  Thanks

Jeff




Answer this question

Move to cell in previous row?

  • furjaw

    ReneeC,  Thank you for the code.

    What i am not sure of is how do i tie this to a button click event

    What i am doing is using the button click to move a cell in one direction (up,down,rigt or left)

    With the following code in my button click event i can move to a new cell, but i need to move in a direction.

                Me.MaintableDataGridView.CurrentCell.Selected = False

                Me.MaintableDataGridView.Rows(0).Cells(0).Selected = True

    I am trying to mimic excel where i can tell it to move one cell by the following:

               Activecell.Offset (0,1).Select      These are the coordinates it moves based on the current location.(in excel VBA)

    I want the literal movement.  (1.go to the next cell up,   2.work with the cell,   3.move move down 1 cell)   etc...

    I want to do this in a loop code (that part i know what to do)  i just can get the move to work.

     

              Me.MaintableDataGridView.CurrentCell.Style.BackColor =Color.Yellow      

              Me.MaintableDataGridView.CurrentCell.Value = ""

    So in the end can you tell me how to directly tell it to move up   Thank you

    Jeff



  • Bob.H

    I have my datagrideview referenced correctly and i added the variables:

    Dim ColumnIndex as Integer

    Dim RowIndex as Integer

    But it does not move the selection. I may be to inexperienced to understand this.

    Why isn't there a method to select cells based on current selection and not based on finding a specific coordinate

    In VB6 i found a Move Method that moves the record pointer.

    Is there a way to find the code that the BindingNavigatorMovePreviousItem button uses (where can i find the code)

    I can't seem to get past the literal working with data cells.

    Thanks Jeff



  • David_G

    If RowIndex > 0 Then

    RowIndex -= 1

    DG1.SelectedCells(0).Selected = False

    DG1.Rows(RowIndex).Selected = True

    e.Handled = True

    End If

    If you put this in a button click event the selectuib will move up everytime you hit the button. if nothing is selected exit the method as I did in the example above.



  • Amadrias

     

    Are the dimension statements inside or outside of a subroutine.

    I mentioned member variables - which mean outside of a subroutine or function.

    Cells have coordnate in terms of rows and columns and must be addressed that way in this control.

     

    It would be beneficial to see your code.



  • ManjuVijay

    Here is the logic for up/down keys

    Right and left would be similar only done with columms and column limits instead

    Public Class Form1

    Private RowIndex As Integer

    Private ColIndex As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _

    Handles Me.Load

    Me.CategoriesTableAdapter.Fill(Me.ModerationDataSet.Categories)

    DG1.MultiSelect = False

    End Sub

    Private Sub DG1_KeyPress(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.KeyPressEventArgs) _

    Handles DG1.KeyPress

    Select Case e.KeyChar

    Case Chr(Keys.Up)

    If RowIndex > 0 Then

    RowIndex -= 1

    DG1.SelectedCells(0).Selected = False

    DG1.Rows(RowIndex).Selected = True

    e.Handled = True

    End If

    Case Chr(Keys.Down)

    If RowIndex < DG1.Rows.Count - 1 Then

    RowIndex += 1

    DG1.SelectedCells(0).Selected = False

    DG1.Rows(RowIndex).Selected = True

    e.Handled = True

    End If

    End Select

    End Sub

    Private Sub DG1_SelectionChanged(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles DG1.SelectionChanged

    If DG1.SelectedRows.Count = 0 Then Exit Sub

    RowIndex = DG1.SelectedCells(0).RowIndex

    ColIndex = DG1.SelectedCells(0).ColumnIndex

    End Sub

    End Class



  • JeevesIndia

    OK   Breakpoint is at first line in selection changed event

    1 Start debug

    2 highlights breakpoint

    3 highlights exit sub

    4 highlights end sub

    5 form load event    highlights

    Me.MaintableTableAdapter.Fill(Me.MainDataSet.Maintable)

    6 highlights

    MaintableDataGridView.MultiSelect = False

    7 highlights   private sub line   (selection change)

    8 shows error message               ContextSwitchDeadlock was detected

    9 highlight breakpoint

    10 highlight exit sub

    11 highlight end sub

    11 form load event   highlights

    MaintableDataGridView.MultiSelect = False

    12 highlights end sub

    13  runs app

    ReneeC  I have to go.   I will return in a few hours.

    Thank you for all your help

    Jeff

     



  • Ghanshyam Singh

     

     

    you need two member variables called rowindex and columindex  = both integers.

     you also need this:

    Private Sub DG1_SelectionChanged(ByVal sender As Object, _
                  
    ByVal e As System.EventArgs) Handles DG1.SelectionChanged

            If DG1.SelectedRows.Count = 0 Then Exit Sub

            RowIndex = DG1.SelectedCells(0).RowIndex

            ColIndex = DG1.SelectedCells(0).ColumnIndex

        End Sub

     

    except dg1  needs to be changed to the name of your datagridview



  • Markm11

    Ok that means that rowindex is = 0 or less than zero.

    Put a break point in the selection changed event.... select a cell single step through that event.

    Do not be afraid to use the locals window or examine the variable values with the debugger. You're not going to hurt anything.



  • J. Clark

    Use the datagridview keypreview event to capture the keys. Look for up and down keys.

    The datagridview is arraigned in terms of rows and columns. For an up key, you want to get the index of the curently selected row and column. and if the row is greater than zero decrement the row index by obe and select the cell.



  • dotnetsekar

    I clicked debug and ran app

    then clicked button

    breakpoint came up in code

    i stepped into it

    it highlighted       (end if)

    then highlighted         (end sub)

    then my running app came back on screen.



  • kewlbuddy

    This is what i have:

    Public Class Mainform

    Private RowIndex As Integer

    Private ColumnIndex As Integer

    Private Sub MaintableBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaintableBindingNavigatorSaveItem.Click

    Dim rowsaffected As Integer = 0

    Me.Validate()

    Me.MaintableBindingSource.EndEdit()

    rowsaffected = Me.MaintableTableAdapter.Update(Me.MainDataSet.Maintable)

    MsgBox(rowsaffected.ToString() & " Rows updated")

    Me.MaintableTableAdapter.Fill(Me.MainDataSet.Maintable)

    End Sub

    Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.MaintableTableAdapter.Fill(Me.MainDataSet.Maintable)

    MaintableDataGridView.MultiSelect = False

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If RowIndex > 0 Then

    RowIndex -= 1

    MaintableDataGridView.SelectedCells(0).Selected = False

    MaintableDataGridView.Rows(RowIndex).Selected = True

    End If

    End Sub

    Private Sub MaintableDataGridView_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MaintableDataGridView.KeyPress

    Select Case e.KeyChar

    Case Chr(Keys.Up)

    If Rowindex > 0 Then

    Rowindex -= 1

    MaintableDataGridView.SelectedCells(0).Selected = False

    MaintableDataGridView.Rows(Rowindex).Selected = True

    e.Handled = True

    End If

    Case Chr(Keys.Down)

    If Rowindex < MaintableDataGridView.Rows.Count - 1 Then

    Rowindex += 1

    MaintableDataGridView.SelectedCells(0).Selected = False

    MaintableDataGridView.Rows(Rowindex).Selected = True

    e.Handled = True

    End If

    End Select

    End Sub

    Private Sub MaintableDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaintableDataGridView.SelectionChanged

    Dim Rowindex As Integer

    Dim ColumnIndex As Integer

    If MaintableDataGridView.SelectedRows.Count = 0 Then Exit Sub

    Rowindex = MaintableDataGridView.SelectedCells(0).RowIndex

    ColumnIndex = MaintableDataGridView.SelectedCells(0).ColumnIndex

    End Sub

    End Class



  • A1Programmer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If RowIndex > 0 Then

    RowIndex -= 1

    MaintableDataGridView.SelectedCells(0).Selected = False

    MaintableDataGridView.Rows(RowIndex).Selected = True

    End If

    In the debugger.... put a breakpoint where it yellow. Start the program in debug mode and press the button. single step through the routine and please describe what happens.



  • MJRP

    I entered the codes exactly as you show them and the selection stays the same.

    The only other thing is that in the button click event code:

    e.handled = true The tip tell me that handled is not a member of System.EventArgs

    i appreciate you helping me since i have only been using VB2005 for about a week.

    I started programming using VB for application in excel.

    Jeff



  • JasonToTheH

    I'm sorry. Eliminate e.handled. It's very early in the morning here. I'm not awake yet.

    I'm glad I could help.



  • Move to cell in previous row?