views:

1617

answers:

2

Hi, On my windows form, I need to programatically set the width of columns in the grid view. I am using the following code:

this.dgridv.Columns[columnName].Width = columnWidth;

The above stmt runs without any error. But, the column width remains unchanged. If I insert a breakpoint and check the value of width after the stmt runs, it is still 100, which I guess is the default value of datagrid column width.

Is there something else I need to do apart from this? Are there any values I need to set before changing the column width?

Any help or pointers on this are highly appreciated.

Thanks :)

+1  A: 

Have you got the AutoSizeColumnsMode set to Fill?

If you have you'll need to set the FillWeight property instead. This isn't a simple width but the proportion of width / no. columns that this column takes up.

If you haven't resized the columns it will be 100.0.

If the column has been widened it will be > 100.0.

If the column has been shrunk it will be < 100.0.

Widening one column, by definition, shrinks the rest.

ChrisF
+1 Thanks for the help :)
Rashmi Pandit
A: 

This worked:

this.dgridv.Columns[columnName].AutoSizeMode= DataGridViewAutoSizeColumnMode.None;
this.dgridv.Columns[columnName].Width = columnWidth;

To reset it back, I am using:

dgvCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
Rashmi Pandit