Hello,
I have this line in BeforeSortColumn and AfterSortColumn event code:
m_LastSortColumn =
Me.UltraGrid1.DisplayLayout.Bands(e.Band.Index).Columns(e.SortedColumns(index).Key)
e.SortedColumn(index).Key correctly reports the Key. e.SortedColumn(index).Index returns -1 instead of the actual column index. Is this a bug in the event args?
I assume you mean BeforeSortChange and AfterSortChange, as the event names you are using here do not exist.
Which event exactly is giving you a -1 for the index? Both?
The SortedColumn collection in the event args represents two different things depending on which event you are using, so it's hard to say without knowing which event you are referring to.
And what index are you passing in?
Is the column visible?
Hello Mike,
Thank you for the response! I apologize for the mistaken event names. You are correct, I meant the Before/AfterSortChange events. The column is visible. I set an UltraGridColumn variable based upon this code:
Me
.UltraGrid1.DisplayLayout.Bands(e.Band.Index).Columns(e.SortedColumns(index).Key)
If I use e.SortedColumns(index).Index, it returns -1. If I use e.SortedColumns(index).Key, it correctly returns the Key. The index variable is just used to iterate the SortedColumns collection and varies from 0 to count - 1.
Shouldn't e.SortedColumns(index).Index return the actual grid column index?
I had to change to use Key in both of the events while iterating the SortedColumns collection.
Thanks for the explanation. I went with the code used because I was actually getting cloning errors when I tried to use the event args collections in earlier attempts.
IndianaDNR said:I was expecting the Index property to be the actual grid column index. It seems logical to me that if e.SortedColumns(index).Key contains the actual grid column Key, then e.SortedColumns(index).Index should contain the actual grid column Index. Instead, e.SortedColumns(index).Index returns -1. That is why I am raising the question as to whether or not this is a bug. We are using WinForms 9.1.
Not necessarily. The Key of a column never really changes, but the Index can change. In this case, the columns you are looking at are most likely not the actual columns from the grid, but rather cloned columns that are passed to you specifically for the purpose of providing you with information in the event args.
In BeforeSortChange, the e.SortedColumns collection is what the new sorted columns collection will look like once the sorting is actually applied to the grid. It can't contain the actual columns from the grid, because those columns have not had their SortIndicator property updated yet. If they did, it wouldn't be BEFORESortChange, it would be after.
You solution of getting the "real" column by using the key of the last column in the SortedColumns collection seems like a good one.
I was expecting the Index property to be the actual grid column index. It seems logical to me that if e.SortedColumns(index).Key contains the actual grid column Key, then e.SortedColumns(index).Index should contain the actual grid column Index. Instead, e.SortedColumns(index).Index returns -1. That is why I am raising the question as to whether or not this is a bug. We are using WinForms 9.1.
I am using the BeforeSortChange event to reset the last grid column width if it was expanded to display the sort glyph. This happens with small numeric data where the header caption is wider than any of the data and the column is auto resized. I use a class level UltraGridColumn to store the last sorted column.
That variable is set up in the AfterSortChange event as below. It works fine with Key. I just happened to set it up initially with Index and got index out of range exceptions, which is what prompted this thread.
m_LastSortColumn = Me.UltraGrid1.DisplayLayout.Bands(e.Band.Index).Columns(e.SortedColumns(index).Key)
It depends which even you are using and what collection you are looking at.
The Index is a property on the column, so this is probably the Index of the column in the grid itself, and not the index of the column in the SortedColumns collection - although I could be wrong about that.But if I am right, then the -1 makes sense, because the column might be in a temporary collection of cloned columns that is created just for the purposes of passing the new sorted columns into the event (or the old sorted columns, depending on which event you are using) and they are not actually in the grid.
Why do you want the index, anyway? What do you need it for?