Share via

DataGridView one-time Autosize.

Bryan Valencia 186 Reputation points
2021-12-02T20:11:16.767+00:00

I have a WinForms app where the DataGridViews break whenever I set all the columns to autosize. I have written a MOUNTAIN of code to try and get around this, and finally just gave up on this feature altogether.

What I'd like to do is this: Right after a "Fill" of my dataset, I want to call some function (which MUST be there) on the DatagridView or its columns that does a one time autosize on all the columns in the result set, but does not leave the columns in the "Autosize" mode.

Lacking that, I guess I'll just have to make all the columns widths save to the user settings. For every column. For every form. And all my users will have to configure all their forms as they use them. This is NOT optimal.

Developer technologies | Windows Forms
0 comments No comments

Answer accepted by question author

Bryan Valencia 186 Reputation points
2021-12-02T20:30:54.34+00:00

Ok, I found this which doesn't crash and seems to work.

        private void ResetWidths()
        {
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
                dataGridView1.AutoResizeColumn(i);
        }

Just call this right after your Fill call.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.