>>I have a grid which displays a checkbook register. The total width of all the columns is less than the total width of the grid, so I want to resize the TransDesc column to fill it in:
>>
>>
>>int iColumnWidths = 0;
>>foreach (DataGridViewColumn oCol in grdRegister.Columns)
>>{
>> iColumnWidths = iColumnWidths + oCol.Width;
>>}
>>grdRegister.Columns["TransDesc"].Width = grdRegister.Width - iColumnWidths;
>>
>>
>>The TransDesc.Width value is negative.
>
>Is TransDesc column part of the grdRegister.Columns? If yes, it's included into the calculation and then, of course, the width would become negative.
Huh? If the total width of the columns IS less than the width of the grid then 'grdRegister.Width - iColumnsWidths' should be positive
>You need to exclude if with if code from the loop.
You wouldn't need to exclude it from the loop - just add the free space to its existing width:
grdRegister.Columns["TransDesc"].Width += (grdRegister.Width - iColumnWidths);
Best,
Viv