Jame Jame - 11 months ago 46
C# Question

How to resize data gridview when the font size changes

I have a datagridview in windows application form below which I put two buttons (one for zoom in and other for zoom out). Now when user press the zoom button I increase/decrease the font size inside the grid according to predefined ratio. It works fine i.e the font size reduces and increases but the only thing that not working is the size of cell or datagridview.

Every time the cell size remains fix. I want to resize my datagridview accordingly, currently i am using the following code but it doesn't work in my case :(

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

Note: my datagridview always contains text in it. So no need to consider other options like what if there is a button/label in a cell etc.

Answer Source

you should either switch the two lines:

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

Or call it like this:


Either way you have to do this after you have changed the font.