Operation can only be performed on cells that belong to a DataGridView control

Posted by The Demigeek on Stack Overflow See other posts from Stack Overflow or by The Demigeek
Published on 2009-09-06T23:10:31Z Indexed on 2010/04/03 23:03 UTC
Read the original article Hit count: 317

Filed under:
|

The following code throws an InvalidOperationException with the above message and I don't understand why. My code calls the following method when the user may have made changes to the datagridview's underlying data source. The goal is to update the display with any changed data, and preserve the sort column and order.

private void ReloadDataGridBindingListFromDatabase()
        {
            DataGridView dgv = myDataGridViewControl;
            DataGridViewColumn sortedColumn = dgv.SortedColumn;
            SortOrder sortOrder = dgv.SortOrder;

            //do stuff here to refresh dgv.DataSource

            if (sortedColumn != null)
            {
                //this line throws an exception
                sortedColumn.HeaderCell.SortGlyphDirection = sortOrder;
            }

            //etc.
        }

Clearly, sortedColumn.HeaderCell is a cell that belongs to a DataGridView control. So why am I getting this exception?

Many thanks for your input.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about datagridview