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

Column sizing in grid

Hi, I have a couple of issues regarding the sizing of columns in a virtual grid; 1) I need to have the right-most column of the grid always fill the remaining area to the right of the control. I''ve seen several posts on this and have this working fine. However, I need to implement a further refinement. The control should fill the right hand space if the total sum of the column widths is less than the client width of the grid. However, the user should be able to size, for example, the first column and have the grid grow a horizontal scroll bar. If the bar is then scrolled all the way to the right, the last column should fill the remaining space. In other words, the grid should allow free sizing of columns by the user, but always stretch the last column to the right-hand client edge of the grid. I don''t seem to able to get this to work within the QueryColWidth method. Any ideas? 2) I want to initialize the widths of the columns, so I set a flag for the first call to QueryColWidth and set the widths of the columns there. However, the grid seems to be resizing them back to the default widths at some later point. Any pointers on the above issues would, as ever, be greatly appreciated.

11 Replies

AD Administrator Syncfusion Team November 2, 2004 08:24 AM UTC

1) Here is a sample. To do this, start your sum from the grid.LeftColIndex instead of 0, and add the row header width separately. This makes the right column width depend upon visible columns. 2) QueryColWidth is completely dynamic. So, any column width you are providing within QueryColWidth is essentially anytime the grid is refreshed. So, if you want to ''set'' a static colwidth value, then you could have a class member hold this value and then use this member in QueryColWidth to provide the value. Then the width would not change until you changed your class member and called grid.Refresh. Now if you are not using QueryColWidth to provide a particular column''s width, then you should be able to set it using grid.COlWidths. Colwidths_3944.zip


MW Mike Woolley November 2, 2004 09:03 AM UTC

Thanks for your really prompt reply. I''ve just tried your sample, and it''s almost, but not quite, what I expect. If you change the sample to have 3 columns the grid appears with the right-most column sized correctly. If you resize col A to fill most of the width of the grid, and then increase the size of B, the scroll bar appears and the last column does indeed fill the remaining width. However, if you run the sample again and resize column B, I would expect the grid to start scrolling and not reduce the size of column C. Hope this makes sense! Regarding the second point - I guess I just need to get the values from the ColWidthsChanged event! Thanks once again.


AD Administrator Syncfusion Team November 2, 2004 09:37 AM UTC

If what you are trying to accomplish is to never see any empty space to the right of the grid, the simplest way to do that is to set grid.HScrollPixel = true. If this does not do what you want, then I think you will have to set a flag to indicate when you want to dynamically size the right column and when you don''t. Then test this flag in QueryColWidth to decide whether to recompute the right col width or to set it to some cached value. It is not clear to me when you want to dynamically size the right column or not. For example, in your scenario of sizing column B, and not dynamically sizing column C, what happens when you scroll to expose column C? DO you want it dynamically sized at that point? Or, do you only want the right col sized once at start up to fill the client area initially, and never size it again? If this is the case, do not handle QueryColWidth. Instead do the calculation in FormLoad and explicitly set the COlWidth at that point.


MW Mike Woolley November 2, 2004 10:33 AM UTC

HScrollPixel didn''t seem to do the trick. However, I can see from your sample how to calculate the sum of the visible column widths. So I guess that all I need to do is resize the last column only if the last column is visible AND the visible column widths are less than the client width of the control? How can I determine whether a column (the last one) is visible?


AD Administrator Syncfusion Team November 2, 2004 11:34 AM UTC

The visible columns are inclusively between grid.LeftColIndex and grid.ViewLayOut.LastVisibleCol.


MW Mike Woolley November 2, 2004 11:57 AM UTC

Thanks once again for your extremely timely and thorough support! I think that''s everything I''ll need to implement this now.


MW Mike Woolley November 3, 2004 10:38 AM UTC

Sorry! Think I may have been a bit hasty there! LastVisibleCol causes the QueryColWidth event to fire and causes a stack overflow... Is there any event that I can hook that occurs after QueryColWidth - if I can explicitly set column widths outside of the QueryColWidth, all I need to do is detect any empty space to the right hand side using int w = grid.ColWidths.GetTotal(grid.LeftColIndex, grid.ColCount) and if grid.ClientRectangle.Width - w > 0 and grid.ViewLayout.LastVisibleCol == grid.ColCount just resize the last column to fill the extra space. Any ideas? Thanks in advance, Mike


AD Administrator Syncfusion Team November 3, 2004 11:53 AM UTC

Maybe you can use the above code from ColWidthsChanged. In your handler size teh right col there if necessary. Make sure you set a flag to avoid any recursive call into ColWidthsChangeded as setting this value will probably trigger this event.


MW Mike Woolley November 3, 2004 11:58 AM UTC

I''ll give that a go. While I''m here, is there any list of new features for the grid products (or indeed, the tools collection) for v3.0 that I can have a look at? Many thanks.


MW Mike Woolley November 3, 2004 11:59 AM UTC

Sorry, seem to have lost some text there - I meant for the forthcoming 3.0 release?


MW Mike Woolley November 3, 2004 01:55 PM UTC

Works a treat! Thanks for the help.

Loader.
Live Chat Icon For mobile
Up arrow icon