Programmatic resizing is useful to avoid performance penalties with continuous resizing. When you programmatically resize columns that have Inherited Auto Size Mode property values of Fill, however, the calculated content-based widths are used to proportionally adjust the column Fill Weight property values, and the actually column widths are then calculated according to these new proportions so that all columns fill the available display area of the control.
You must then call the Auto Resize Columns method to adjust the column widths to the calculated ideal.
For all sizing enumerations, values with names that refer to displayed cells limit their calculations to cells in displayed rows.
With these values, you can limit which cells are used to calculate the preferred sizes.
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post.
Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.