Solved

DataGridView not reflecting current row in RowStateChanged Event

Posted on 2014-10-18
3
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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