We help IT Professionals succeed at work.

Datagridview - RowEnter

TSFLLC
TSFLLC asked
on
I seem to keep fighting this irritating problem while navigating a datagridview.

One thing VB.NET does with datagridviews that to me is such a crock is that it always highlights the first row.
Why?  When you've got 4-5 datagridviews on a form, you load the form and you're sitting there looking at all of these selected rows...and for what?

So you have to unselect rows.
grdview.CurrentCell = grdview.Item(1, 0)
grdview.CurrentCell.Selected = False

I am trying to get a currentrow index on my datagridview whenever one of two things happens:
1)  User clicks a row.
2) User arrows up and down changing the rowindex.

The RowEnter property apparently fires prior to the row index changing.  I keep getting the first row based on unselecting the default row OnLoad.
I need the NEW row index.

What property do I have to use to capture the new row index whenever there is movement between rows?

Thanks.  Phil
Private Sub grdviewWebQueue_RowChg(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdviewWebQueue.RowEnter
        If Activating Then Exit Sub
        If dvWebQueue.Count > 0 Then
            Dim msg As String = ""
            Dim index As Integer = grdviewWebQueue.CurrentRow.Index
            If (index = -1) Then
                OutofHere = True
                Exit Sub
            Else
                glQueueID = dvWebQueue(index)("pkid").ToString
            End If
        End If
    End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
I was originally using SelectionChanged, but the same thing happens.

The routine I am attempting to perform is a delete from a table & refresh of the datagridview while maintaining the selected row near the deleted row.

1) Delete from table using SQL statement.
2) Refresh the dataview & datagridview.
3) If not row 0, select the row prior to the deleted row.

#3 is shown below.  When debugging, I watch it execute these two lines & then the grdviewWebQueue.SelectionChanged fires.  But the CurrentRow.Index = 0 when it should be 3 or 4.
grdviewWebQueue.CurrentCell.Selected = True
grdviewWebQueue.CurrentCell = grdviewWebQueue.Item(1, x)

STRANGELY ENOUGH, when you look at the form upon completion of the process, the right row is selected/highlighted in blue.  It's just that the CurrentRow.Index is right when SelectionChanged fires.


I have the gridview set to select FullRow but I'm convinced I've had no problem in the past with setting a cell selected & it selects the row.

Author

Commented:
MISTAKE

>>CurrentRow.Index is NOT right when SelectionChanged fires.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
When the grid is refreshed, first row is selected automatically. You need to add a step 0 before step 1.

0) Store the index of currently selected row to a temp variable
1)...
2)...
3) Select the row which is at index-1 from step 0.

Author

Commented:
CodeCruiser, I just tested thoroughly.

I put a Msgbox(grdview.CurrentRow.ToString) in my Sub right after where it sets CurrentCell above.  CurrentRow is correct at that point.  grdview.RowEnter/SelectionChanged does not reflect an updated CurrentRow yet.

I resolved by duplicating a piece of code I needed to execute from RowEnter/SelectionChanged and added it below CurrentCell code.  Everything works.  I've just realized that RowEnter/SelectionChanged gives proper CurrentRow if you click with mouse or move up/down with arrows.  Programmatically, when setting CurrentCell...CurrentRow isn't correct where RowEnter/SelectionChanged is fired.

Author

Commented:
I don't need to save the index because I loop at bottom of cmdDelete...  to set the right row.
Below is my code.
Private Sub cmdDeleteQueueRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteQueueRec.Click
        Dim glTemporaryContactID As Int64 = 0
        If grdviewWebQueue.SelectedRows.Count = 1 Then
            Dim msg As String = ""
            Dim index As Integer = grdviewWebQueue.CurrentRow.Index
            If (index = -1) Then
                Beep()
                msg = "Valid web queue record must be selected!!"
                MessageBox.Show(msg, "Stop!!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                OutofHere = True
                Exit Sub
            Else
                msg = "This record will be removed from web queue record list." & vbCrLf & "Are you sure you want to delete this record?"
                If MessageBox.Show(Me, msg, "Deleting Current Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If
                Try
                    glQueueID = dvWebQueue(index)("pkid").ToString
                    Activating = True
                    DeleteQueueRec()
                    GetWebQueueRecs()
                    If dvWebQueue.Count > 0 Then
                        If index = 0 Then
                            glQueueID = dvWebQueue(0)("pkid").ToString
                        Else
                            glQueueID = dvWebQueue(index - 1)("pkid").ToString
                        End If
                        ' PERFORM FIND ON RECORD
                        For x As Int32 = 0 To dvWebQueue.Count - 1
                            If dvWebQueue(x)("pkid").ToString = glQueueID Then
                                grdviewWebQueue.CurrentCell = grdviewWebQueue.Rows(x).Cells(1) 'Item(1, x)
                                grdviewWebQueue.CurrentCell.Selected = True
                                Exit For
                            End If
                        Next
                        'Activating = False
                        Dim index2 As Integer = grdviewWebQueue.CurrentRow.Index
                        GetPossibleMatches(dvWebQueue(index2)("contact_id"), dvWebQueue(index2)("contact_index").ToString, dvWebQueue(index2)("last_name").ToString, dvWebQueue(index2)("email").ToString)
                    End If
                    Activating = False
                Catch ex As Exception
                    HandledExceptionManager.EmailError = True
                    HandledExceptionManager.ShowDialog(glUserInitials, ex, GetEmailDefaults(glCompany, glUserInitials))
                    glValueUpdated = False
                    glSavedEvent = True
                    Me.Close()
                End Try
            End If
        End If
    End Sub


    Private Sub grdviewWebQueue_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdviewWebQueue.SelectionChanged
        If Activating Then Exit Sub
        If dvWebQueue.Count > 0 Then
            Dim msg As String = ""
            Dim index As Integer = grdviewWebQueue.CurrentRow.Index
            If (index = -1) Then
                OutofHere = True
                Exit Sub
            Else
                glQueueID = dvWebQueue(index)("pkid").ToString
            End If
            GetPossibleMatches(dvWebQueue(index)("contact_id"), dvWebQueue(index)("contact_index").ToString, dvWebQueue(index)("last_name").ToString, dvWebQueue(index)("email").ToString)
            If dvMemberMatch.Count > 0 Then
                grdviewMemberMatch.CurrentCell = grdviewMemberMatch.Item(1, 0)
                grdviewMemberMatch.CurrentCell.Selected = False
            End If
        Else
            GetPossibleMatches(-1, "", "", "")
        End If
    End Sub

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
If you are setting the currentcell programmatically, you already know the rowindex don't you?

Author

Commented:
I just realized I can remove several lines of code based on your last post.
' PERFORM FIND ON RECORD
                        For x As Int32 = 0 To dvWebQueue.Count - 1
                            If dvWebQueue(x)("pkid").ToString = glQueueID Then
                                grdviewWebQueue.CurrentCell = grdviewWebQueue.Rows(x).Cells(1)
                                grdviewWebQueue.CurrentCell.Selected = True
                                GetPossibleMatches(dvWebQueue(x)("contact_id"), dvWebQueue(x)("contact_index").ToString, dvWebQueue(x)("last_name").ToString, dvWebQueue(x)("email").ToString)                                
                                Exit For
                            End If
                        Next

Open in new window

Author

Commented:
Sometimes you just need to bounce things around.  But it can be a critical issue if your're anticipating RowEnter/SelectionChanged to reflect the 'current' row when setting CurrentCell programmatically.

Wish I knew of a Property that didn't fire until ALL updates occurred to a gridview.
Thanks!

Author

Commented:
This is a little cleaner.  You may have been eluding to this previously.  No For/Next required.....
Private Sub cmdDeleteQueueRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteQueueRec.Click
        If grdviewWebQueue.SelectedRows.Count = 1 Then
            Dim msg As String = ""
            Dim i As Integer = grdviewWebQueue.CurrentRow.Index
            If (i = -1) Then
                Beep()
                msg = "Valid web queue record must be selected!!"
                MessageBox.Show(msg, "Stop!!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                OutofHere = True
                Exit Sub
            Else
                msg = "This record will be removed from web queue record list." & vbCrLf & "Are you sure you want to delete this record?"
                If MessageBox.Show(Me, msg, "Deleting Current Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If
                Try
                    Activating = True
                    DeleteQueueRec()
                    GetWebQueueRecs()
                    If dvWebQueue.Count > 0 Then
                        Dim i2 As Integer = 0
                        If i = 0 Then
                            'i2 defaults to 0.
                        Else
                            i2 = i - 1
                        End If
                        glQueueID = dvWebQueue(i2)("pkid").ToString
                        grdviewWebQueue.CurrentCell = grdviewWebQueue.Rows(i2).Cells(1)
                        grdviewWebQueue.CurrentCell.Selected = True
                        GetPossibleMatches(dvWebQueue(i2)("contact_id"), dvWebQueue(i2)("contact_index").ToString, dvWebQueue(i2)("last_name").ToString, dvWebQueue(i2)("email").ToString)
                    Else
                        GetPossibleMatches(-1, "", "", "")
                    End If
                    Activating = False
                Catch ex As Exception
                    HandledExceptionManager.EmailError = True
                    HandledExceptionManager.ShowDialog(glUserInitials, ex, GetEmailDefaults(glCompany, glUserInitials))
                    glValueUpdated = False
                    glSavedEvent = True
                    Me.Close()
                End Try
            End If
        End If
    End Sub

Open in new window