Hi,
I encountered a bug that in the event of AfterColPosChanged, the VisiblePosition returned from e.ColumnHeaders(0).VisiblePosition is wrong.
Example:
In the grid i have column1, column2 and column3 (Left to Right).
I removed Column2.
Drag Column1 into Column3 position.
Now in the AfterColPosChanged event, e.ColumnHeaders(0).VisiblePosition is 2, which is WRONG. It should be 1, because there is only 2 visible columns (zero based index)
Please help.
Refresh works for me but not invalidate. I think that should do the trick, let me sort around things in my program and see if I can make it work. Thanks for your help.
It looks like this is just a timing issue. The VisibleHeaders has not been updated when AfterColPosChanged fires. If you put a button on the form and call RefreshGrid in your sample and then move the column and then click the button, the VisibleHeaders is correct.
So I think the problem here is that the VisibleHeaders collection is updated asynchronously - probably the next time the grid paints.So the solution is simple - you need to force the grid to paint before you check the VisibleHeaders.You can do this by adding a call to Grid.Refresh before you access the VisibleHeaders collection. I tried this out and it works fine. In fact, it works for me when I call grid.Invalidate, so that's probably the better option.
Private Sub RefreshGrid() C1.Text = "" C2.Text = "" C3.Text = "" ' Invalidate the grid so that the VisibleHeaders are up-to-date. UltraGrid1.Invalidate() For i As Integer = 0 To UltraGrid1.ActiveColScrollRegion.VisibleHeaders.Count - 1 Select Case i Case 0 C1.Text = UltraGrid1.ActiveColScrollRegion.VisibleHeaders(i).Header.VisiblePosition.ToString Case 1 C2.Text = UltraGrid1.ActiveColScrollRegion.VisibleHeaders(i).Header.VisiblePosition.ToString Case 2 C3.Text = UltraGrid1.ActiveColScrollRegion.VisibleHeaders(i).Header.VisiblePosition.ToString End Select Next End Sub
Attached sample project. Try drag and drop Column1 to Column3 position. Looping thru collection of ActiveColScrollRegion.VisibleHeaders will give you VisiblePosition 2-0-1 instead of 0-1-2 with the actual column item being swapped around in the collection.
I guess the grid column still stay at the same place as in the collection item, only the VisiblePosition has changed. So, how can I can sort them out easily without having me copy over them to an array and sort it myself. Thanks
I will try to compose a sample project in a bit.
I must keep the visibleposition in sequence without gap because i have to maintain the columns list (top to bottom = left to right on the grid) in few other listview controls (similar to fieldchooser). User has the ability to make changes on the listview with drag and drop function to reorder/add/delete columns without accessing the grid. Changes make on the listview and grid will reflect each other. If grid sequence has gap, i can't reorder the listview correctly without the exact position shown on the listview (top to bottom = left to right on the grid). I considered to delete all the items on the listview and reconstruct them based on the fresh list of grid columns, but that I required the collection of grid columns to be in order ===> sample project
caspers said:I cannot save the invisible columns. Reason: I have to toss them away and not save into database so that later time when user load the grid and the columns that were invisible should not be exists at all.
Okay... but why does it matter that there are gaps in the VisiblePosition values? As I mentioned before, the VisiblePositons do not have to be in sequence.
You could save this:
Column A = 0
Column B = 10
Column C = 15
And restore the same values and it will work fine. The only tricky part of this is that you must set the VisiblePositions in order from lowest to highest - but you would have to do that regardless of whether the numbers are consecutive or not.
caspers said:VisiblePosition NOT in order meaning when I walked thru the collection, it shows VisiblePosition as (2, 0, 1) from item 1 to item N (left to right columns on the grid). Which is not what I want, I need it to be 0, 1, 2.... etc. Also, again the visiblePosition still included the hiddencolumns but if the collection items are in order, i can easily fix it myself, problem is it's not in order.
I've never seen this happen - unless maybe you are using RowLayouts - in which case, the VisiblePosition property is not used.
If you can duplicate this in a small sample project and post it here, I'd love to take a look at it.