DisplayIndex Is Already Used For Another Column

Apr 2, 2009 at 3:20 PM
I have multiple datagrids (all of the same type) displaying in my application.  When the user selects an option to sort the columns it fires off an event that each grid is listening too and they rearrange their columns based on a dictionary that is passed in and looping through to setup their new DisplayIndexes.  This first time a user sorts it works just find, but the second time they sort it throws an exception the third instance of the grid that the sort event handler is called, saying that the displayindex is already used for another column.  The other thing to note is that each instance of the grid is on a seperate ElementHost object hosted in a windows forms application.  Is there a bug with the displayIndex property on a column, or do I need to do something differently?


 public void SortHandler(object sender, SortArgs e)
        {
           //If dictionary passed in matches size of dictionary of current column order
            if (e.ColumnHeaderSortDictionary.Count == myColumnOrder.Count)
            {

                //for each item offset by three as there are three fixed columns at the beginning of the grid
                for (int i = 3; i < e.ColumnHeaderSortDictionary.Count + 3; i++)
                {
                    String ge;
                    if (e.ColumnHeaderSortDictionary.TryGetValue(i, out ge))
                    {
                         //find current position
                        int currentIndex = myColumnOrder[ge.ToUpper()];
                         //update index
                        dgMatrix.Columns[currentIndex].DisplayIndex = i;
                        //update dictionary of current column order
                        myColumnOrder[ge.ToUpper()] = i;


                    }
                }

                dgMatrix.UpdateLayout();
            }
        }