DataGridView not reflecting current row in RowStateChanged Event

Posted on 2014-10-18
Last Modified: 2014-10-20
I have a DataGridView that utilizes the RowStateChanged Event. I basically have textboxes that are associated with cells in the DGV. So, when the user uses the arrow key in the DGV depending on what row the cursor is on, that row's data will be reflected in the TextBoxes. For example, for a specific record, there are 4 rows that populate the DGV. In the RowStateChanged Event, I have code that will assign a specific Cell to a TextBox. After the DGV is loaded, I set the CurrentCell to the 1st Cell, 1st Row, however, the TextBoxes reflect data from the last row of data, in this case 4 or 3 if you go by zero index. I ran it thru the debugger and the RowStateChanged Event actually fires first before I set the CurrentCell position.

So, my question is how or where do I set the CurrentCell so the RowStateChanged Event will recognize it's new position?


Here is my code below.

    Private Sub dgvEquipmentItems_RowStateChanged(sender As Object, e As DataGridViewRowStateChangedEventArgs) Handles dgvEquipmentItems.RowStateChanged
            If blnIsLoaded Then
                txtSeqNO.Text = CStr(BL.CheckForNULLs(e.Row.Cells(1).Value))
                txtManufacturer.Text = CStr(BL.CheckForNULLs(e.Row.Cells(3).Value))
                txtComponentSerialNO.Text = CStr(BL.CheckForNULLs(e.Row.Cells(4).Value))
                txtItemNO.Text = CStr(BL.CheckForNULLs(e.Row.Cells(5).Value))
                txtModelNO.Text = CStr(BL.CheckForNULLs(e.Row.Cells(6).Value))
                txtChannel.Text = CStr(BL.CheckForNULLs(e.Row.Cells(8).Value))
                txtCapacity.Text = CStr(BL.CheckForNULLs(e.Row.Cells(11).Value))
                txtCapacityUnit.Text = CStr(BL.CheckForNULLs(e.Row.Cells(13).Value))
                txtLowerRange.Text = CStr(BL.CheckForNULLs(e.Row.Cells(15).Value))
                txtUpperRange.Text = CStr(BL.CheckForNULLs(e.Row.Cells(16).Value))
                txtRangeUnit.Text = CStr(BL.CheckForNULLs(e.Row.Cells(18).Value))
            End If

        Catch ex As Exception
            EH.ErrorMessage = "dgvEquipmentItems_RowStateChanged() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    Private Sub LoadDetailGrid()
            BL.GetDETAILSEquipmentRecord(iEquipment_ID, dbDETAILS_Equipment, EH)

            dgvEquipmentItems.DataSource = Nothing

            If EH.ErrorMessage = String.Empty Then
                If EH.DataSet.Tables(0).Rows.Count > 0 Then
                    rowCnt = EH.DataSet.Tables(0).Rows.Count
                    dgvEquipmentItems.DataSource = EH.DataSet.Tables(0)
                    dgvEquipmentItems.Columns(0).Visible = False
                    dgvEquipmentItems.Columns(2).Visible = False
                    dgvEquipmentItems.Columns(7).Visible = False
                    dgvEquipmentItems.Columns(9).Visible = False
                    dgvEquipmentItems.Columns(12).Visible = False
                    dgvEquipmentItems.Columns(13).Visible = False
                    dgvEquipmentItems.Columns(17).Visible = False
                    dgvEquipmentItems.Columns(18).Visible = False
                    dgvEquipmentItems.Columns(19).Visible = False
                    dgvEquipmentItems.Columns(20).Visible = False
                    dgvEquipmentItems.Columns(21).Visible = False
                    dgvEquipmentItems.Columns(22).Visible = False
                    dgvEquipmentItems.Columns(23).Visible = False
                    dgvEquipmentItems.Columns(24).Visible = False
                    dgvEquipmentItems.Columns(25).Visible = False
                    dgvEquipmentItems.Columns(26).Visible = False
                    dgvEquipmentItems.Columns(27).Visible = False
                    dgvEquipmentItems.Columns(28).Visible = False
                    dgvEquipmentItems.Columns(29).Visible = False
                    dgvEquipmentItems.CurrentCell = dgvEquipmentItems.Rows(0).Cells(1)
                End If
            End If
    End Sub

Open in new window

Question by:BlakeMcKenna
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 40389906
RowStateChanged occurs on many different situations. The fact that the row is selected is only one of those. So you are not using the right event for what you want to do if I understand correctly.

Try with RowEnter instead.
LVL 83

Expert Comment

ID: 40391726

Author Closing Comment

ID: 40391970

RowEnter did the trick!


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now