Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

Set to a record in Datagridview after refresh

I have Datagridview on a form (frmGridData). A user double-clicks on a record in the grid to open another form (frmGridDataDetails) to make changes to the data. After the record has been edited, the user clicks save. The form (frmGridDataDetails) closes and the grid refreshs on the frmGridData form to show the data change in the grid. How do I get the grid to show (or select) the record (row) that was just edited. I've been reading about currenymanager, however, I have no idea how to use it.

ADawn
0
ADawn
Asked:
ADawn
  • 4
  • 3
1 Solution
 
CyberLexCommented:
Greetings

maybe you are looking for this?

frmGridData.FirstDisplayedScrollingRowIndex = integer

this will make the gridview scroll to the rowindex, but only if its not already in view

cheers
Alex
0
 
ADawnAuthor Commented:

Alex,

frmGridData is the form name. The grid on teh form is DGV_Names. Do you mean
DGV_Names.FirstDisplayedScrollingRowIndex = integer ?

Thanks, ADawn
0
 
CyberLexCommented:
yea, messes those names up :)

datagridviewname.FirstDisplayedScrollingRowIndex = x

x is the row number/integer


Hope it helped :)
Lex
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ADawnAuthor Commented:
OK I tried your code, however, I need it to select a row by it unique number (autoid) of the record. I have records added to a sql database with an auto increment field (AutoID). I need to be able to (select or highlight) the record (row) within the datagridview after the record has been updated (edited).

The column is AutoID

Thanks, ADawn
0
 
CyberLexCommented:
Cheers

posted code below by using just the datagridview, but of course if you need example 2 i'd suppose you could do that directly on the database to find the highest AutoID.

- "Column1" is your AutoID Column-Name

cheers
alex

        'example 1 assuming the hightest number in AutoID is the same
        'as rowcount (total number of rows) if you start your AutoID
        'also with 1 as the first entry
        'if thats not the case see example 2
        For Each row As DataGridViewRow In DataGridView1.Rows
 
            If row.Cells("Column1").Value = DataGridView1.RowCount Then
 
                MsgBox(row.Index.ToString)
                row.Selected = True
                DataGridView1.FirstDisplayedScrollingRowIndex = row.Index
                Exit For
 
            End If
 
        Next
        'example 1 end
 
 
 
 
        '*****************************
        'example 2
        'get all the value into an array
        Dim ar(DataGridView1.RowCount - 1) As Integer
 
        For Each row As DataGridViewRow In DataGridView1.Rows
 
            ar(row.Index) = row.Cells("Column1").Value
 
        Next
 
        'get the highest number in the array
        Dim highest As Integer = GetLargeInt(ar)
 
        For Each row As DataGridViewRow In DataGridView1.Rows
 
            If row.Cells("Column1").Value = highest Then
 
                MsgBox(row.Index.ToString)
                row.Selected = True
                DataGridView1.FirstDisplayedScrollingRowIndex = row.Index
                Exit For
 
            End If
 
        Next
 
 
 
Private Function GetLargeInt(ByVal data() As Integer) As Integer
 
        Array.Sort(data)
 
        Array.Reverse(data)
 
        Return data(0)
 
End Function
 
        'end example 2

Open in new window

0
 
ADawnAuthor Commented:
Hello alex,
I'm sorry for the late reply - been on vacation. I'm not understanding what you're doing. Let me explain better. Say I do a search for books that start with the word 'Green' . I click a button and 228 items are shown in the datagridview. Now, I double-click on a row (autoId = 578) to open that record for editing in another form. Once the editing has been completed, the edit form is closed and the grid on the orginal form is refreshed. Now, I want to set the focus back on the record with the autoid of 578. How can I do that?

Thanks for your help and patience. - ADawn
0
 
CyberLexCommented:
in that case propably the easiest thing to do is to iterate through the AutoID cells and if you find your desired row to select and display do so.

this is just an example, of course you will need to replace 578 with the integer variable holding the last edited AutoID and the "AutoID" is the columnname, which might be different in your case.

cheers
Alex

For Each row As DataGridViewRow In DGV_Names.Rows
 
            If row.Cells("AutoID").Value = 578 Then
                row.Selected = True
                DGV_Names.FirstDisplayedScrollingRowIndex = row.Index
                Exit For
            End If
 
Next

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now