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

后端 未结 4 1208
旧巷少年郎
旧巷少年郎 2021-02-15 00:51

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:09

    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
    

提交回复
热议问题