Solved

DataGridView not reflecting current row in RowStateChanged Event

Posted on 2014-10-18
3
315 Views
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?

Thanks,

Here is my code below.

    Private Sub dgvEquipmentItems_RowStateChanged(sender As Object, e As DataGridViewRowStateChangedEventArgs) Handles dgvEquipmentItems.RowStateChanged
        Try
            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()
            InitializeErrorClass(EH)
            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

0
Comment
Question by:BlakeMcKenna
3 Comments
 
LVL 40

Accepted Solution

by:
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40391726
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40391970
James,

RowEnter did the trick!

Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

13 Experts available now in Live!

Get 1:1 Help Now