Results 1 to 18 of 18

Thread: [RESOLVED] DataGridView - Keep at current row after update

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Michigan
    Posts
    304

    Resolved [RESOLVED] DataGridView - Keep at current row after update

    I need some help keeping a DataGridView at the current row while being updated. I have about 140 rows of data that is being update with engineering data every 100 msec.

    If the user is looking at a row 80, the slider is in the middle, when my LoadDt() runs to update with the latest data, the grid jumps back to row 1. I tried getting the first cell displayed and setting that back when done but this is not working.

    Any suggestions on keeping the grid at the current row when updated?

    Code:
        Private Sub LoadDt(ByVal Chrono As String)
            Dim FirstDisplayedCell As DataGridViewCell = DataGridView1.FirstDisplayedCell 'get first cell showing
    
            'load the data table with the latest data
            '::::
            '::::
            '::::
            '::::
    
            'set the grid back the the first cell displayed
            DataGridView1.CurrentCell = FirstDisplayedCell
        End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: DataGridView - Keep at current row after update

    Moved From The CodeBank (which is for sharing code rather than asking questions )

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Michigan
    Posts
    304

    Re: DataGridView - Keep at current row after update

    My bad

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,283

    Re: DataGridView - Keep at current row after update

    You need to save the address of the cell and not the cell itself.
    Try this:
    Code:
     Private Sub LoadDt(ByVal Chrono As String)
            'Save the address of the 1st display cell
            Dim col As Integer = DataGridView1.FirstDisplayedCell.ColumnIndex
            Dim row As Integer = DataGridView1.FirstDisplayedCell.RowIndex
    
            'load the data table with the latest data
            '::::
            '::::
            '::::
            '::::
    
            'set the grid back the the first cell displayed
            DataGridView1.FirstDisplayedCell = DataGridView1.Item(col, row)
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Michigan
    Posts
    304

    Re: DataGridView - Keep at current row after update

    Yes, worked well

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: DataGridView - Keep at current row after update

    I have tried to implement the code listed in this post with my refresh procedure with no luck
    i get "Object reference not set to an instance of an object." as an error

    what am i missing....

    (I apologize very new to vb and first attempt at a program)

    Thanks.

    Sub Refreshdg1()

    Dim sql As String
    Dim sql2 As String
    sql = "SELECT * FROM Table1"
    sql2 = "SELECT * FROM Table2"
    Dim adapter As New OleDbDataAdapter(sql, con1)
    Dim adapter2 As New OleDbDataAdapter(sql2, con1)

    Dim col As Integer = Main.dg1.FirstDisplayedCell.ColumnIndex
    Dim row As Integer = Main.dg1.FirstDisplayedCell.RowIndex

    Dim dt As New DataTable("Table1")
    Dim dt2 As New DataTable("Table2")

    adapter.Fill(dt)
    adapter2.Fill(dt2)

    Main.dg1.FirstDisplayedCell = Main.dg1.Item(col, row)


    Dim dv As New DataView
    dv.Table = dt
    dv.Sort = "Date"
    Main.dg1.DataSource = dv


    Dim dv2 As New DataView
    dv2.Table = dt2
    dv2.Sort = "Date"
    Main.dg2.DataSource = dv2

    I guess I also have a bonus question my sorting works great if I am the only user in the program, but if i try and run multiple .exe's at the same time and one user adds an item the sorting gets out of whack.


    Thanks in advance..

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: DataGridView - Keep at current row after update

    I'm not sure if it matters but I am using oledbdataconnection

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,283

    Re: [RESOLVED] DataGridView - Keep at current row after update

    Always indicate which line in your code the error occurs... That will help us in narrowing down the possible causes.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    error occurs: Dim col As Integer = Main.dgv1.FirstDisplayedCell.ColumnIndex

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,283

    Re: [RESOLVED] DataGridView - Keep at current row after update

    Does dgv1 has any rows when you run that code? If it has no rows then obviously there are no cells to display and therefore no FirstDisplayedCell.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    I use the same code on my main formload "refreshdg1()" ... so that makes sense ... let me attempt to fill the datagridview first then rerun it...

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    I the error is now gone, and the program executes.....

    I am trying to make the program so that when the refreshdv1() procedure runs off a timer tick
    the current selected row will still have focus so to speak.... and not return to the first row

    Thanks!

  13. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    *edit
    Last edited by wb1605; Aug 1st, 2012 at 11:15 AM.

  14. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    *edit
    Last edited by wb1605; Aug 1st, 2012 at 11:16 AM.

  15. #15
    Junior Member
    Join Date
    Jul 2012
    Posts
    23

    Re: [RESOLVED] DataGridView - Keep at current row after update

    Yes ! i got the code to work, but now i am trying to get the current row instead of cell to be shown after refresh i tried

    Dim showrow As Integer = Main.dgv1.CurrentRow.Index

    Main.dgv1.CurrentRow = Main.dgv1.CurrentRow(showrow)

    which will not work because currentrow is readonly

    any suggestions ?

    Thanks.

  16. #16
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,283

    Re: [RESOLVED] DataGridView - Keep at current row after update

    If you want to highlight the whole row, you have to select the row. Being selected and being current are not the same. That is, a row can be selected (highlighted) without being the current row and vice-versa. The datagridview CurrentRow property is readonly. When you set the CurrentCell property, the CurrentRow is automatically set.

    To highlight a row, you set its Selected property to true:
    Code:
    DGV.Rows(rowIndex).Selected = True
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  17. #17
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,678

    Re: [RESOLVED] DataGridView - Keep at current row after update

    Quote Originally Posted by wb1605 View Post
    Yes ! i got the code to work, but now i am trying to get the current row instead of cell to be shown after refresh i tried

    Dim showrow As Integer = Main.dgv1.CurrentRow.Index

    Main.dgv1.CurrentRow = Main.dgv1.CurrentRow(showrow)

    which will not work because currentrow is readonly

    any suggestions ?

    Thanks.
    If you where to create a BindingSource, set the DataSource to the DataTable then assign the DataGridView.DataSource to the BindingSource this is easy. Right before re-loading data you get the primary key for the current row, remember the key value, reload the DataGridView via the BindingSource. Once loaded using the Find method of the BindingSource which you pass the column name for the primary key and the remembered value for the key. If the Find method returns anything greater than -1 use the BindingSource Position property to assign the index returned from the Find Method. This will position you to the row you want.

    The following has a working project which loads a MS-Access database table on form load. When exiting the row is remembered so that next time the app starts the row is restored. This is similar to what you want, just that in your app you are not starting/ending/starting but loading/reloading.

    Project link post #10
    Last edited by kareninstructor; Aug 1st, 2012 at 08:52 AM. Reason: Added link to another post

  18. #18
    New Member
    Join Date
    Jul 2019
    Posts
    1

    Re: DataGridView - Keep at current row after update

    how can i use this code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width