Reputation: 23838
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 :)
Upvotes: 0
Views: 12373
Reputation: 395
dataGridView1.Columns[index].Width = 150;
This line of code will set the column width programatically.
Upvotes: 0
Reputation: 23838
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;
Upvotes: 0
Reputation: 137178
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.
Upvotes: 2