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

后端 未结 4 936
暖寄归人
暖寄归人 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:00

    This seems to be a bug - the code is trying to access dataGridView.TopLeftHeaderCell, which when happens for the first time actually creates that cell and triggers some layout actions not expected at that moment.

    With all that in mind, the fix is simple. We need to make sure that the TopLeftHeaderCell is created before DataGridView handle, by adding the following line (before affffding the grid to Controls for instance)

    var topLeftHeaderCell = grid.TopLeftHeaderCell; // Make sure TopLeftHeaderCell is created
    

提交回复
热议问题