Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

Adjusting column width of datagridview according to content size

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 ?

Upvotes: 0

Views: 1157

Answers (2)

Géry Arduino
Géry Arduino

Reputation: 490

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

Upvotes: 0

Bedouin
Bedouin

Reputation: 490

You can use :

gridview.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

Upvotes: 1

Related Questions