开发者

Unable to Restore display index in datagridview

So I have a class that I use to filter the results of a datagridview in vb.net. And one of the things I want this class to do, is keep the column order, width, and visibility.

So what I do, is when my search class is instantiated, i create a copy of the columns in a hashtable, for later referencing.

Private dgv As DataGridView 
Private dataSet As DataSet 
Private bindingSource As New BindingSource

Dim ht As New Hashtable()

Public Sub new( ByRef dgv As DataGridView, ByVal dataSet As dataset )

    Me.dgv = dgv 
    Me.dataSet = dataSet 

    For Each col As DataGridViewColumn In dgv.Columns 
        ht.Add( col.headertext, col ) 
    Next

End Sub

Then, I filter it like so...

Public Sub quickFilter( ByVal searchterm As string,  ByVal tablename As String, optional ByVal colname As string = Nothing )

    dgv.DataSource = dataSet 
    dgv.DataMember = tablename 

    bindingSource.DataSource = CType( dgv.DataSource, DataSet ).Tables.Item( tablename ) 

    bindingSource.Filter = determineColumnsFilter( searchterm, colname ) 

    dgv.DataSource开发者_Python百科 = bindingSource.DataSource 

    restoreCols() 

End Sub

The confusing part, is the restore cols function correctly restores width, and visibility, but not the display index. It seems to shuffle the columns around in any which way it chooses.

Private Sub restoreCols()

    For Each col As DataGridViewColumn In dgv.Columns 

        Dim colcopy As DataGridViewColumn = ht( col.headertext )

        col.Width = colcopy.Width 
        col.Visible = colcopy.Visible 
        col.DisplayIndex = colcopy.DisplayIndex 

    Next

End Sub

Any idea why the display indexes don't restore properly?

Thanks


I found a work around is to have a second hashtable that stores the display index. For some reason, the way I was doing it originally, the display index was changing. But not the other values I was referencing.


You have to loop through the columns in the order of the display order, starting at zero; otherwise the columns will fall out of order as the loop progresses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜