locked
Select recently added row in DataGridView? RRS feed

  • Question

  • I have a main form that contains a datagridview and a button. The button hides the main form and calls a second form where the user can enter some data. When the second form is closed, the data is put into a row object that is added to the DGV and the main form is shown. My problem is that I want the just added row to be selected when the main form comes back.

     

    This is the last thing I tried:

    Code Snippet

    For i = 0 To Me.RTDBDataSet.CandidateInfo.Rows.Count - 1

    Me.RTDBDataSet.CandidateInfo.Rows(i).Item(1).ToString()

    If Me.RTDBDataSet.CandidateInfo.Rows(i).Item(1) = strRecordNumber Then

    Me.dgvMain.Rows(i).Selected = True

    Exit For

    End If

    Next

     

     

    And it doesn't work.  Well, it works if the DGV is not sorted in any way. So I guess what I'm asking for help with to get that last-added row to be selected no matter how the DGV is sorted...

     

    Thanks for any help!

     

    Wednesday, May 7, 2008 4:00 AM

Answers

  • Hi pkirill,

     

    Based on my understanding, you want to highlight the row just added in the DataGridView control. If I have misunderstood you, please feel free to tell me, thanks.

     

    I think you can do it like this: First, iterates the DataGridView Rows and to find the row you just added. Then select it.

    See the following example.

     

    Code Snippet

     

    dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

    foreach(DataGridViewRow dr in dgv.Rows)

    {

    if(someCondition)

    {

    dr.Selected = true;

    }

    }

     

     

     

     

    Regards,

    Roy Zhou

    Tuesday, May 13, 2008 5:58 AM