views:

171

answers:

2

The project on which I'm currently working has two datagridviews that are meant to compare two versions of similar data (same number and name of columns). I'd like to resize all the columns so that they fit two criteria:

1) Autosize to fit data (that's easy) 2) So the columns are lined up such that for any column COL, gridA[COL].Width = Max(gridA[COL].Width, gridB[COL].Width), and the same for gridB[COL].Width

The problem I'm encountering is, when you do an auto-resize, the Width property doesn't show you the ACTUAL width (it displays the width before the auto-resize, I guess).

A: 

For auto-resize there's the FillWeight property.

If you set both gridviews' AutoSizeColumnsMode to Fill, you can read the FillWeight off the first and apply it to the second. You'll probably have to do it for all the columns though.

ChrisF
Yes. Let me clarify - I can auto-size it fine; that part was added for completeness. But if the AutoSizeMode that is affecting the column is not "None", the Width displayed is not the "actual" width.I need to be able to do something like set the widths of the cols on the second grid to those of the first one (which are autosized)
A: 

I found another way to resize columns that allows for more control. I can't believe I missed it the first time around.

One can use the DataGridViewColumn.GetPreferredWidth(...) method to get the autosize width

Can't believe I missed that one as well.
ChrisF