Adjusting column width of datagridview according to content size

1.1k Views Asked by At

Guys by specifying the column index,It is possible to set the autoresize property of that column. Like,

 gridview.AutoResizeColumn(1);

Is there any way to set this property to the datagridview without having to specify the columns indexes ?

2

There are 2 best solutions below

0
On BEST ANSWER
0
On

i would suggest you to set that value at creation time like :

Column myColumn = new Column() { AutoResizeColumn = true };

And then add this column to the column list of your gridview

on another hand you can do a "for each" loop to loop all the columns and give them the appropriate param value :

for each (Column col in gridview.Columns) {
    col.AutoResizeColumn = true;
}

but doing this at initialisation is probably a much stable approach (like having a column template object) and if you need to change value then you call by index this way is suppose :

gridview.Columns[index].AutoResizeColumn = false //as you wish