-
Jan 20th, 2011, 01:28 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jan 20th, 2011, 01:35 PM
#2
Re: DataGridView - Keep at current row after update
Moved From The CodeBank (which is for sharing code rather than asking questions )
-
Jan 20th, 2011, 02:17 PM
#3
Thread Starter
Hyperactive Member
Re: DataGridView - Keep at current row after update
My bad
-
Jan 20th, 2011, 03:01 PM
#4
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 -
-
Jan 21st, 2011, 09:33 AM
#5
Thread Starter
Hyperactive Member
Re: DataGridView - Keep at current row after update
Yes, worked well
-
Jul 31st, 2012, 01:56 PM
#6
Junior Member
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..
-
Jul 31st, 2012, 02:56 PM
#7
Junior Member
Re: DataGridView - Keep at current row after update
I'm not sure if it matters but I am using oledbdataconnection
-
Jul 31st, 2012, 03:09 PM
#8
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 -
-
Jul 31st, 2012, 03:29 PM
#9
Junior Member
Re: [RESOLVED] DataGridView - Keep at current row after update
error occurs: Dim col As Integer = Main.dgv1.FirstDisplayedCell.ColumnIndex
-
Jul 31st, 2012, 03:33 PM
#10
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 -
-
Jul 31st, 2012, 03:40 PM
#11
Junior Member
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...
-
Jul 31st, 2012, 03:47 PM
#12
Junior Member
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!
-
Jul 31st, 2012, 04:35 PM
#13
Junior Member
Re: [RESOLVED] DataGridView - Keep at current row after update
Last edited by wb1605; Aug 1st, 2012 at 11:15 AM.
-
Jul 31st, 2012, 04:37 PM
#14
Junior Member
Re: [RESOLVED] DataGridView - Keep at current row after update
Last edited by wb1605; Aug 1st, 2012 at 11:16 AM.
-
Aug 1st, 2012, 08:25 AM
#15
Junior Member
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.
-
Aug 1st, 2012, 08:40 AM
#16
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 -
-
Aug 1st, 2012, 08:44 AM
#17
Re: [RESOLVED] DataGridView - Keep at current row after update
 Originally Posted by wb1605
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
-
Jul 22nd, 2019, 08:30 PM
#18
New Member
Re: DataGridView - Keep at current row after update
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|