Go Premium for a chance to win a PS4. Enter to Win


DataGridView not reflecting current row in RowStateChanged Event

Posted on 2014-10-18
Medium Priority
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

926 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