One way would be to make a public sub on the form with the datagridview to move the highlight and call this sub from the buttons on your other form.
The sub could look like:
Public Sub ChangeRow(ByVal UpDown As Boolean)
' UpDown True moves Up
' UpDown False moves Down
Dim Row As Integer = DGV1.CurrentRow.Index
Dim Column As Integer = DGV1.CurrentCell.ColumnIndex
If Row = 0 And UpDown Then Exit Sub
If Row = DGV1.RowCount - 1 And Not UpDown Then Exit Sub
If UpDown Then
DGV1.CurrentCell = DGV1(Column, Row - 1)
Else
DGV1.CurrentCell = DGV1(Column, Row + 1)
End If
End Sub
So I have been giving it a try but it seems when I try to get back to the datagrid screen from the details screen the col and row properties are set back to 0 for the gridview object.
Actually they go to 0 when the grid is out of focus.
Public Sub ChangeRow(ByVal UpDown As Boolean)
' UpDown True moves Up
' UpDown False moves Down
Static Row As Integer = DGV1.CurrentRow.Index
Static Column As Integer = DGV1.CurrentCell.ColumnIndex
If Row = 0 And UpDown Then Exit Sub
If Row = DGV1.RowCount - 1 And Not UpDown Then Exit Sub
If UpDown Then
DGV1.CurrentCell = DGV1(Column, Row - 1)
Else
DGV1.CurrentCell = DGV1(Column, Row + 1)
End If
End Sub
Regards,
S_DS
Sorry still confused.
The RowCount is the number of rows in your datagrid - so are you saying that they all disappear.
The currentcell rowindex and columnindex are readonly properties of the grid whose values change as the user navigates round the grid. You cannot fix them or declare them as shared member variables.
I think it would help if you were to post some code so we can see what you are doing.
Datagrid form:
Private Sub dgvDatagrid_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDatagrid.CellDoubleClick Dim strSubID As String = dgvViewSubscriber.SelectedCells.Item(1).Value.ToString End Sub Here is the function as per your suggestion. Public Sub ChangeCursor(ByVal boolUp As Boolean) If m_intGridRow = 0 And boolUp Then Exit Sub If boolUp Then End Sub
m_intGridRow = dgvViewSubscriber.CurrentCell.RowIndex
m_intGridCol = dgvViewSubscriber.CurrentCell.ColumnIndex
...
...
...
Dim detailsForm As new DetailsForm
detailsForm.show()
....
...
If m_intGridRow = dgvDatagrid.RowCount - 1 And Not boolUp Then Exit Sub
dgvDatagrid.CurrentCell = dgvDatagrid(m_intGridCol, m_intGridRow - 1)
Else
dgvDatagrid.CurrentCell = dgvDatagrid(m_intGridCol, m_intGridRow + 1)
End If
Details Form:
Private Sub NextClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click....
Dim frmGrid As DatagridForm
frmGrid.GetForm()
' My code to get the current opened form. I also tried without the function as well. It is used to allow single instance of the form.
frmGrid.ChangeCursor(false)
....
End Sub
So this throws a ArgumentOutOfRange Exception. While debugging the properties for the datagrid are 0. However when the grid form is in focus the properties such as rowcount are back to 100 which they should be. I hope this gives some sort of idea of what I am trying to do.
OK - I can see a couple of problems there.
1. You are storing the currentcell row and column index in m_intGridRow and m_intGridRow. However when you set them in the cell double click event you are setting them to the values of a different datagrid (dgvViewSelection). So the first change would be to change those two lines to:
m_intGridRow = dgvDataGrid.CurrentCell.RowIndex
m_intGridCol = dgvDataGrid.CurrentCell.ColumnIndex
2. However it still won't work because you are not updating these values when the selection changes. So you need to handle the CurrentCellChanged event as well so that they are kept up to date. e.g.
Private Sub dgvDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvDataGrid.CurrentCellChanged
m_intGridRow = dgvDataGrid.CurrentCell.RowIndex
m_intGridCol = dgvDataGrid.CurrentCell.ColumnIndex
End Sub
In fact once you have that event handled you don't need to set the values in the double click event. Having done this it works ok for me.
I tried using the currentcellchanged event and it seems that event gets raised when the grid loads and when I try to set m_intGridRow and m_intgridCol I get a nullReferenceException for the dvgDataGrid object. {"Object reference not set to an instance of an object."}
Thanks for all the help and bearing with me.
The currentcellchanged event is only raised when the current cell is changed. Setting the m_intGridRow and m_intgridCol values in the currentcellchanged event cannot produce the exception you are experiencing.
Are you trying to set these values somewhere else, such as the Form load event - that will produce the exception your are seeing. I cannot think of any reason why you would need to set these values other than in the cellchanged event so try removing the code that is setting them elsewhere and see if that solves the problem. If not then please post all the code where these values are being set.