views:

30

answers:

1

I'm having a windows form. In this form I've datagrid control which has few columns with predefined width.

When I resize the form either by using maximize box or using mouse, datagrid automatically resizes itself to fit the form. This is done using anchor property of the datagrid.

Now I need to resize the width of the columns as well so that all the columns fit within resized grid without getting horizontal scroll bar. At present I'm doing this by calculating the ratio of the new grid width with the old grid width and increasing the column width in the same ratio. But the problem in this is the ratio is not accurate when I maximize and minimize the form, so after couple of resize actions total columns width is less than the grid width and it starts showing empty space in the grid. I'm doing this in grid resize event.

Another catch in this is, there are couple of fixed columns also present in this grid and I'm not supposed to change the width of those columns on grid resize.

How should we handle this? Is there any other way to handle this resize problem?

Update: I was doing unnecessary job of calculating the ratio to increase the width of the columns, which is not necessary.

Thanks @KMan. Your suggestion to use the fill property worked. It takes care of column width resizing based on the grid changed width on form resize.

+1  A: 

How about:

Also, checkout How to: Set the Sizing Modes of the Windows Forms DataGridView Control.

KMan
@KMan: Thanks for the answer. I'm not supposed to increase the width if the value of the cell doesn't fit in it. I should resize the column only on resize happens to the grid. I think both the properties mentioned in your first point relates to the inner width of the column with respect to value of the cell.
JPReddy
@KMan: Your second point worked when I modified other properties of the grid. Thanks.
JPReddy