InvalidOperationException: This operation cannot be performed while an auto-filled column is being resized

后端 未结 4 934
暖寄归人
暖寄归人 2021-02-15 00:30

I have a form with a DataGridView and I want to set the columns AutoSizeMode to Fill and the grids ColumnHeadersHeightSizeMode

4条回答
  •  暖寄归人
    2021-02-15 01:04

    Thank you, Ulf, for the excellent sample showing how to reproduce this. One of my clients reported this bug to me and your sample has been invaluable.

    Taking Ivan's excellent answer one step further, creating your own grid inheriting from the DataGridView should prevent this ridiculous bug permanently. Just be sure to always use the custom grid throughout your application.

    public class Grid
        : DataGridView
    {
        protected override void OnHandleCreated(EventArgs e)
        {
            // Touching the TopLeftHeaderCell here prevents
            // System.InvalidOperationException:
            // This operation cannot be performed while
            // an auto-filled column is being resized.
    
            var topLeftHeaderCell = TopLeftHeaderCell;
    
            base.OnHandleCreated(e);
        }
    }
    

提交回复
热议问题