We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Auto sizing columns

I have been trying to autosize my columns based on internal settings for fixed and variable width columns. My variable width column also does text wrapping, so the Autosize property doesn''t help me (or so it seems to me). If I understand correctly, the autosizing done by the grid itself only can be width or height but not both. If I am correct, I think that is a mistake. I have a column that may contain a lot of text, so I want it to take up as much width as is available and then autosize the height based on how much text wrapped. Anyway, I am attempting to do it myself and my calculations are off a little bit. If I do this: public void CheckColSizes() { int iCol = myGrid.ColCount; int iFixed = 0; while (iCol > 0) { iCol--; if( !myGrid.HideCols[iCol]) { if( iCol != myVariableColumn) { iFixed += myGrid.ColWidths[iCol] + 10; } } } int iVarWidth = myGrid.ClientSize.Width - iFixed; if( iVarWidth < myVariableMin) iVarWidth = myVariableMin; myGrid.ColWidths[myVariableColumn] = iVarWidth; myGrid.RowHeights.ResizeToFit(Syncfusion.Windows.Forms.Grid.GridRangeInfo.Table()); } I end up with the last column partially out of the window. I thought it had to do with the scroll bar at first, but it doesn''t (same problem if there is no vertical scroll bar). It would seem that I need to add borders or what not. How can I correct this? -Andy

4 Replies

AB Andy Barnhart February 18, 2004 01:47 PM UTC

oops, I left the " + 10" in there while I was just fooling around testing to see if it was fixed in my particular grid. It is not. 10 works for 4 columns, but not for other numbers of columns. I wasn''t plannig to use a literal number; just checking to see if it was a literal number.

AD Administrator Syncfusion Team February 18, 2004 04:24 PM UTC

Here is a little sample using your idea. Size the form before clicking the button to see it work to fill us the new client region of the grid. forum10969_1800.zip The only tricky part is that calling RowHeights.ResizeToFit has the potential to make the vertical scrollbar appear if it is not already visible since increasing the hieght of the cells may mak eit so not all rows are visible. So, to handle this, one option is to just always make it visible to avoid this problem.

AB Andy Barnhart February 18, 2004 04:45 PM UTC

I got that to work with a couple of minor alterations. If the row headers are not shown, it still includes them in the calculation, so it wasn''t filling the window. I had to change the 0 to a 1 in the calculation of usedWidth to the left. That worked. It works okay with other columns being hidden though. The other thing I do is call my function to check the row sizes in response to scroll bar visibility changing as well as resize. -Andy

AB Andy Barnhart February 18, 2004 10:49 PM UTC

Just wanted to make sure you realized there was a bug report (I think) in that last message. I mean the bit about the row headers (AKA column 0) being included in the width even if they aren''t shown. Hopefully, that can be fixed before 2.0 is released.

Live Chat Icon For mobile
Up arrow icon