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

Resizing column header dynamically

I''ve searched the forum and seen a lot of stuff about resizing column headers, but couldn''t find exactly what I needed. I''m running latest version. I have a virtual grid. The user can specify the column header text he wants, and when he changes it, I want to change the text and resize the column. I want it to be big enough to hold the text, but I don''t want it to be smaller than some reasonable default width. In the leave event of the textbox I update my underlying data source and then do this: grid.InvalidateRange(GridRangeInfo.Cell(0, col)); grid.ColWidths.ResizeToFit(GridRangeInfo.Col(col), GridResizeToFitOptions.IncludeHeaders); Problems: 1) I notice the tooltip for ResizeToFit says "deprecated". Why? 2) The column doesn''t resize right away. It does resize later after some other event (like I tab back to the control and out again). 3) This doesn''t handle my requirement of having a reasonable default width. Jim

15 Replies

AD Administrator Syncfusion Team September 24, 2005 09:42 PM UTC

Try calling grid.ColWidths.ResizeToFit(GridRangeInfo.Col(col), GridResizeToFitOptions.IncludeHeaders); grid.Refresh(); to see if this handles this problem for you. The deprecated comment is only for the method which accepts two bools as the 2nd and 3rd arguments. This method has been replaced with the one that uses the GridResizeToFit enums.


JI Jim September 25, 2005 05:10 PM UTC

Excellent, thanks for the quick response, Clay! I just discovered the DefaultColWidth property. ResizeToFit might resize a column to something less than this value. Is there a way to query what value ResizeToFit will use before calling it? If not I guess it''s not a big deal, I can just call ResizeToFit and then if I don''t like it I can set the column width explicitly with ColWidths[col] before calling Refresh(). Jim


AD Administrator Syncfusion Team September 25, 2005 05:45 PM UTC

There is no way to query ResizeToFit to see what size it will set. You can prevent the size from decreasing by adding the GridResizeToFitOptions.NoShrinkSize to the second parameter in the call.


JI Jim September 25, 2005 09:51 PM UTC

I saw that option, but I don''t think it''s quite what I want. Let''s say the user changes the text to something long, and then something short. The second time I assume with that option that the col width wouldn''t shrink. I don''t want the col width to not shrink, I just want to maintain a reasonable minimum width. Here''s what I came up with. This is the Leave event for the text box where the user can change the name of the column. private void uiName_Leave(object sender, System.EventArgs e) { TraceEntity trace = (TraceEntity)traceSet.Trace[currentlySelectedTrace]; if (trace.Name != uiName.Text) { trace.Name = uiName.Text; int col = currentlySelectedTrace + 1; // A reasonable minimum width would be the default width or something less // if the user has resized column himself. int minWidth = Math.Min(uiTracesGrid.ColWidths[col], uiTracesGrid.DefaultColWidth); uiTracesGrid.ColWidths.ResizeToFit(GridRangeInfo.Col(col), GridResizeToFitOptions.IncludeHeaders); if (uiTracesGrid.ColWidths[col] < minWidth) uiTracesGrid.ColWidths[col] = minWidth; uiTracesGrid.InvalidateRange(GridRangeInfo.Col(col)); uiTracesGrid.Refresh(); } } Unfortunately, I''m still having a couple of problems. I change the text of a column to "Total Borehole Volume" and the column header text changes, but the column does not resize (the text is doubled up over two lines). If I change it again (just add a letter, let''s say), then it''s fine. Other problem is between the time I tab out of my textbox and the grid updates it takes one full second. Bit more of a delay than I would like. Here''s my code for GridQueryCellInfo, just so you can see how I''m filling the grid. At any one time there''s only about 8 columns and 8 rows visible. The grid fills with data quickly, just for some reason the resizing as I''ve implemented it seems slow (and doesn''t always work). Any ideas? Thanks Jim


JI Jim September 25, 2005 09:55 PM UTC

Clay, I realized after I posted that message I probably don''t need the line uiTracesGrid.InvalidateRange(GridRangeInfo.Col(col)); so I took it out, but it doesn''t make much difference on the time. I also realize that you won''t know exactly what the cost is for my virtual GridQueryCellInfo code, I just include that for the sake of completeness. Again -- that code is not otherwise slow so far as I know of.


JI Jim September 25, 2005 10:06 PM UTC

Clay, Sorry for all the posts :) But I do have some more specific info that may help us solve the problem. I change a column header from "BVOL" to "Total Borehole Volume". My code fires. uiTracesGrid.ColWidths.ResizeToFit(GridRangeInfo.Col(col), GridResizeToFitOptions.IncludeHeaders); sets the width to 50! When I change the text a second time, then same code sets the width to 142 (which of course is correct). ? Jim


AD Administrator Syncfusion Team September 25, 2005 11:42 PM UTC

What code are you using to set the column header text into the grid? And where do you have this code?


JI Jim September 26, 2005 08:31 PM UTC

I''m handling the Leave event of a textbox on the same form as the grid. This is my code again (entire function this time). private void uiName_Leave(object sender, System.EventArgs e) { // special case: we want column header to update as soon as user changes trace name TraceEntity trace = (TraceEntity)traceSet.Trace[currentlySelectedTrace]; if (trace.Name != uiName.Text) { trace.Name = uiName.Text; int col = currentlySelectedTrace + 1; // A reasonable minimum width would be the default width or something less // if the user has resized column himself. int minWidth = Math.Min(uiTracesGrid.ColWidths[col], uiTracesGrid.DefaultColWidth); uiTracesGrid.ColWidths.ResizeToFit(GridRangeInfo.Col(col), GridResizeToFitOptions.IncludeHeaders); if (uiTracesGrid.ColWidths[col] < minWidth) uiTracesGrid.ColWidths[col] = minWidth; uiTracesGrid.Refresh(); } }


JI Jim September 27, 2005 05:47 PM UTC

Clay, any ideas yet? Jim


AD Administrator Syncfusion Team September 27, 2005 08:04 PM UTC

How are you setting the header text? I do not see how you are moving the value your user types into the header text for a column. There is no code shown like: this.gridDataBoundGrid1.GridBoundColumns["col"].HeaderText = "????";


JI Jim September 27, 2005 08:55 PM UTC

It''s a virtual grid. Sorry about that, in one of my previous posts I said I was showing you the code for GridQueryCellInfo, but it looks like I didn''t successfully paste in the code. So in the Leave event I update the Name property on a trace object and update the col widths, as you''ve seen. Here''s my GridQueryCellInfo: private void GridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { if (e.RowIndex == 0 && e.ColIndex > 0) { TraceEntity trace = (TraceEntity)traceSet.Trace[e.ColIndex - 1]; e.Style.CellValue = trace.Name; e.Handled = true; } else if (e.RowIndex > 0 && e.ColIndex > 0) { TraceEntity trace = (TraceEntity)traceSet.Trace[e.ColIndex - 1]; e.Style.CellValue = trace[e.RowIndex - 1].ToString(trace.PlacesFormat); e.Handled = true; } } Jim


AD Administrator Syncfusion Team September 27, 2005 10:15 PM UTC

Try this. Immediately after setting trace.Name = uiName.Text; Add a call to uiTracesGrid.Model.ResetVolatileDate();


JI Jim September 27, 2005 10:56 PM UTC

Thanks, Clay. If I put that there then it works fine, and I can remove the call to Refresh() apparently as well. My only remaining complaint is that is that there is a noticeable delay (about 1 sec) for the grid column to resize. There''s no flicker, everything looks fine, but just that delay. Even if I remove if (uiTracesGrid.ColWidths[col] < minWidth) uiTracesGrid.ColWidths[col] = minWidth; so that I''m not resizing twice, it''s still slow. Anything I can do to help that? If not, I guess I''ll have to live with it. Thanks, Jim


AD Administrator Syncfusion Team September 28, 2005 09:18 AM UTC

If you have a lot of rows, then GridRangeInfo.Col(col) inside the ResizeToFit call can take signficant time. If it will serve your needs, you could restrict the call to the visible range using GridRangeInfo.Cells(grid.TopRowIndex, col, grid.ViewLayout.LastVisibleRow, col). This will likely be much quicker if you have many rows.


JI Jim September 28, 2005 03:41 PM UTC

Ah, yes, I do have a lot of rows. I was assuming it was only using visible rows, but of course I am telling it explicitly what range to look at there! I changed the range as you instructed and it is now quite snappy. Looks like it all works the way I want it now. Thanks for all the help! Jim

Loader.
Live Chat Icon For mobile
Up arrow icon