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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Resizing column header dynamically

Thread ID:

Created:

Updated:

Platform:

Replies:

35166 Sep 24,2005 05:43 PM UTC Sep 28,2005 03:41 PM UTC WinForms 15
loading
Tags: GridControl
Jim
Asked On September 24, 2005 05:43 PM UTC

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

Administrator [Syncfusion]
Replied On 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.

Jim
Replied On 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

Administrator [Syncfusion]
Replied On 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.

Jim
Replied On 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

Jim
Replied On 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.

Jim
Replied On 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

Administrator [Syncfusion]
Replied On 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?

Jim
Replied On 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(); } }

Jim
Replied On September 27, 2005 05:47 PM UTC

Clay, any ideas yet? Jim

Administrator [Syncfusion]
Replied On 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 = "????";

Jim
Replied On 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

Administrator [Syncfusion]
Replied On September 27, 2005 10:15 PM UTC

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

Jim
Replied On 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

Administrator [Syncfusion]
Replied On 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.

Jim
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;