We help IT Professionals succeed at work.

how to maintain order in datagridview in vb.net

PNRT
PNRT asked
on
684 Views
Last Modified: 2012-05-06
Hi
I have a simple datagridview that is cleared with

DGV1.Rows.Clear()

and then updated on a timer with

For Each LC As DataStore In Update.APV.Values
DGV1.Rows.Add(New String() {DGVID, DGVWhen, DGVToGo})
Next

This works fine but If the user has changed the cell order in the columns, this is altered when the update takes place, as the order reverts back to the original.  Also the datagridview reverts back to cell 1,1

Is there a way of maintaining the cell order and cell position required by the user as the update takes place?

Thanks
Comment
Watch Question

Commented:
I always used this method, to in sure that even the user change the order of the column, the code still inserting on the right cell.

'So instead of this...

For Each LC As DataStore In Update.APV.Values
    DGV1.Rows.Add(New String() {DGVID, DGVWhen, DGVToGo})
Next

I use:

For Each LC As DataStore In Update.APV.Values
Dim rowIndex as integer = DGV1.Rows.Add

DGV1(colDGVID.index, rowindex).value = DGVID
DGV1(colDGVWhen.index, rowindex).value = DGVWhen
DGV1(colDGVToGo.index, rowindex).value = DGVToGo

Next

Although this is longer compared to the code you are using right now, I am sure that even the user change the order of the column, the code still inserting on the right cell.

I hope I could help you,

Jack
'Suggested code:
 
For Each LC As DataStore In Update.APV.Values
Dim rowIndex as integer = DGV1.Rows.Add
 
DGV1(colDGVID.index, rowindex).value = DGVID
DGV1(colDGVWhen.index, rowindex).value = DGVWhen
DGV1(colDGVToGo.index, rowindex).value = DGVToGo
 
Next

Open in new window

Author

Commented:
Thank you for your quick reply
I have tried the code as shown above, but it still does the same thing.   The column order still
reverts back to what it was before the user changed it, and also the highlighted row reverts
back to row 1.  This is the code I used.  I cant really see how this would maintain the order
required by the user.  Did I make my question clear that it is not incorrect cells that is the
problem, but keeping the same order of the cells in the column and also the
highlighted row kept in the same position and not moved back to row 1.  Many Thanks for
your help.

Private Sub UpdateDGV(ByVal DGVID As String, ByVal DGVWhen As String, ByVal DGVToGo As String)
        Dim rowIndex As Integer = DGV1.Rows.Add
        DGV1(Column1.Index, rowIndex).Value = DGVID
        DGV1(Column2.Index, rowIndex).Value = DGVWhen
        DGV1(Column3.Index, rowIndex).Value = DGVToGo
    End Sub

Commented:
Does AllowColumnReorder is set to true?

Commented:
Did you set the datagridview property AllowColumnReorder = True?

Try also this code snippet...
Private Sub UpdateDGV(ByVal DGVID As String, ByVal DGVWhen As String, ByVal DGVToGo As String)
        Dim selectedRowIndex as integer = 0
        Dim rowIndex As Integer = DGV1.Rows.Add
        If DGV1.SelectedRows.Count <> 0 thne
              SelectedRowIndex = DGV1.SelectedRows(0).index
        End If
 
        DGV1(Column1.Index, rowIndex).Value = DGVID
        DGV1(Column2.Index, rowIndex).Value = DGVWhen
        DGV1(Column3.Index, rowIndex).Value = DGVToGo
 
        DGV1.Rows(SelectedRowIndex).Selected  = True
    End Sub

Open in new window

Author

Commented:
Thank you very much for replying so quickly, unfortuantely still the same problem.

Is the problem perhaps in the way I am clearing DGV1 in the first place ie
 DGV1.Rows.Clear()
Could that be the problem?

AllowUserToOrderColumns is set to True
This is the code so far

Private Sub UpdateDGV(ByVal DGVID As String, ByVal DGVWhen As String, ByVal DGVToGo As String)
        Dim selectedRowIndex As Integer = 0
        Dim rowIndex As Integer = DGV1.Rows.Add
        If DGV1.SelectedRows.Count <> 0 Then
            selectedRowIndex = DGV1.SelectedRows(0).Index
        End If
        DGV1(Column1.Index, rowIndex).Value = DGVID
        DGV1(Column2.Index, rowIndex).Value = DGVWhen
        DGV1(Column3.Index, rowIndex).Value = DGVToGo
        DGV1.Rows(selectedRowIndex).Selected = True
End Sub

Commented:
Put this code after you clear the contents of the Datagridview.

  Dim selectedRowIndex As Integer = 0
        If DGV1.SelectedRows.Count <> 0 Then
            selectedRowIndex = DGV1.SelectedRows(0).Index
  End If


Then put this after you update all the contents of the datagridview...


DGV1.Rows(selectedRowIndex).Selected = True

Author

Commented:
Still the same unfortunately.

Just to experiment,  I started a new project just with a timer and a Datagridview, the code is below.
This has the same problem.  Ist column goes back to original order and highlighted row moves to 1

Imports System.Data.OleDb

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Interval = 5000
        DGV1.Rows.Clear()
        Dim selectedRowIndex As Integer = 0
        If DGV1.SelectedRows.Count <> 0 Then
            selectedRowIndex = DGV1.SelectedRows(0).Index
        End If

        Dim i As Integer
        For i = 1 To 20

            dgvid = i
            dgvwhen = ""
            dgvtogo = ""
           
            DGV1.AllowUserToOrderColumns = True
            Dim rowIndex As Integer = DGV1.Rows.Add
            If DGV1.SelectedRows.Count <> 0 Then
                selectedRowIndex = DGV1.SelectedRows(0).Index
            End If
            DGV1(Column1.Index, rowIndex).Value = dgvid
            DGV1(Column2.Index, rowIndex).Value = dgvwhen
            DGV1(Column3.Index, rowIndex).Value = dgvtogo
            DGV1.Rows(selectedRowIndex).Selected = True
        Next
    End Sub

Commented:
Try this one...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 
        Timer1.Interval = 5000
 
Dim selectedRowIndex As Integer = 0
        If DGV1.SelectedRows.Count <> 0 Then
            selectedRowIndex = DGV1.SelectedRows(0).Index
        End If
        DGV1.Rows.Clear()
        
 
        Dim i As Integer
        For i = 1 To 20
 
            dgvid = i
            dgvwhen = ""
            dgvtogo = ""
           
            DGV1.AllowUserToOrderColumns = True
            Dim rowIndex As Integer = DGV1.Rows.Add
            If DGV1.SelectedRows.Count <> 0 Then
                selectedRowIndex = DGV1.SelectedRows(0).Index
            End If
            DGV1(Column1.Index, rowIndex).Value = dgvid
            DGV1(Column2.Index, rowIndex).Value = dgvwhen
            DGV1(Column3.Index, rowIndex).Value = dgvtogo
            
        Next
DGV1.Rows(selectedRowIndex).Selected = True
    End Sub

Open in new window

Author

Commented:
Hi again
Sorry but still the same thing.
Are we understanding the same problem in this question?
With the program at present, column 1 is populated  1 to 20.  
When I click on the column header the order changes to 20 to 1.  
When I click on say cell 5, that cell becomes highlighted.  
This is all corect.  
However, when the next update takes place, the order reverts back to 1 to 20 and the highlighted
cell becomes 1 again.  
Im trying to get it so that the column order remailns the same and the highlighted cell and position remain the same.

Is that OK?

Many Thanks
Kevin RobinsonPrivate VB.NET Contractor

Commented:
"Is there a way of maintaining the cell order and cell position required by the user as the update takes place?"

Yes of course.  I am working on this at the minute and is not as hard as i sounds.  My code is not quite finished but you could set a property of my control to a user setting of your application. This would alllow you user to select the columns they want to see, the width and the positions of the columns This would be loaded back every time the app is started.

Author

Commented:

I didnt hear back from anyone as yet?   Perhaps this is not possible?

Private VB.NET Contractor
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Kevin RobinsonPrivate VB.NET Contractor

Commented:
Did that work ok for you?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.