Autosize columns with minimal width

Jun 18, 2009 at 11:48 AM


I've a little problem: I'm trying to define minimal column width and aslo use autosize. I mean I would like that my columns auto-adjust with content width, but with a minimal width. My columns should have a defined width, but auto-expand if it's not large enough.

I tried with  "grid.Columns[#].AutoSizeMode = SourceGrid.AutoSizeMode.MinimumSize" and "grid.AutoSizeCells()" but it deosn't work.

What am I making incorrectly? Sould I use "grid.AutoStretchColumnsToFitWidth = true" or "grid.Columns.StretchToFit()" or "this.grid.Columns.AutoSizeColumn()" or ... ?

I don't really understand what's the difference between all these method and I couldn't deduce it from the samples. Could somebody help?


Jun 18, 2009 at 6:43 PM

You can use

grid.Columns[#].MinimalWidth = X;

and use grid.Columns[#].AutoSizeMode = SourceGrid.AutoSizeMode.Default;

Jun 19, 2009 at 1:45 PM
Edited Jun 19, 2009 at 2:13 PM

Originally I posted that autostretch was not working,but that was my error.

I did raise a bug that the autosize is a bit messed up with the scrolling/sizing and the width of a scroll bar.

I also do not like how there are too many public variables and functions to do autosizing


column.autosizemode, grid.autosize, grid.columns.autosize, rows.autosize, grid.autosizecells(), grid1.autostretchcolumnstofitwidth, columns,.strechtofit() etc.

Really, you should just have to set the properties on the cells, and be done with it... IMO.


Jun 22, 2009 at 1:15 PM

Thanks Lafriks, but it's doesn't work.

I think I'm doing a mistake somewhere. Does this work with colomn spanning? And in this cas, do I need to define every MinimalWidth?

In the example above, I've defined .MinimalWidth = # and .AutoSizeMode = ...Default only on the non-spanned lines (the ones with blue columns).

The other question is: I define these parameters (MinimalWidth and AutoSizeMode) at the beginning of my grid creation and I fill the cells after that. Is the AutoSize automatically applied after that or do I need to execute a refresh or apply the AutoSize manually later?


Jun 23, 2009 at 6:16 AM

Nice Grid!

Looks like you are pushing SourceGrid a bit further, then what is demonstrated in standard examples.

Jun 23, 2009 at 6:33 AM

Well, I'm trying to do an "universal" editor based on XML format definition, for geographic (coordinates) purposes. And til now I'm very happy with SourceGird and the mulit-header and spanning possibilities.
I'm still experiencing some problems like Sorting (very slow), AutoSize and some little things, but it seams to be a very good control.

But any idea why the column width isn't big enough (eg blue "Est" and "Nord") although I used "MinimalWidth" and "AutoSizeMode" ?

Jun 23, 2009 at 2:00 PM

Sorry, but i myself am actually not that experienced in UI stuff.  

You could try debug it, and see why this happens.

Or if you coulds strip down your project and send it to me, i could try debug and see what is happening in there.


I could also look at why sorting is slow. Perhaps it is because you are using Grid, not DataGrid, and internal sorting algorithm isn't just suited for this kind of data manipulation.  When i was improving performance for DataGrid, i noticed that soring 1 million rows with dataTable.Sort() takes a couple of seconds. When i looked at the method names, i saw methods, which suggest that some kind of tree structure is used there. I am not expert in trees, but i believe it is not just a simple binary tree.


If you could give an example where sorting is extremely slow (say, takes at least 10 seconds), i could profile with dotTrace  and tell you what is causing the problem


Jun 23, 2009 at 4:40 PM

OK, I found that .MinimalWidth defined that a column cannot be narrower that this value.
It's not what I want, I only want that the cell width is extended if his content isn't integrally displayed.

I'll try to play in debug mode to find out how it works I'll perhaps be able to give you more details or pupose a new solution.


Jun 25, 2009 at 11:17 AM

About sorting, it's relatively fast with 100 lines, but with 10000 rows (sort on Doubles) it takes 25 seconds on my computer.

And when I use spanning, I become an Exception:

System.InvalidOperationException was unhandled
  Message="Failed to compare two elements in the array."
       at System.Array.SorterObjectArray.QuickSort(Int32 left, Int32 right)
       at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer)
       at SourceGrid.Grid.OnSortingRangeRows(SortRangeRowsEventArgs e)
       at SourceGrid.GridVirtual.SortRangeRows(Range p_Range, Int32 keyColumn, Boolean p_bAscending, IComparer p_CellComparer)
       at SourceGrid.Cells.Controllers.SortableHeader.SortColumn(CellContext sender, Boolean p_bAscending, IComparer p_Comparer)
       at SourceGrid.Cells.Controllers.SortableHeader.OnMouseUp(CellContext sender, MouseEventArgs e)
       at SourceGrid.Cells.Controllers.ControllerContainer.OnMouseUp(CellContext sender, MouseEventArgs e)
       at SourceGrid.Cells.Controllers.CellEventDispatcher.OnMouseUp(CellContext sender, MouseEventArgs e)
       at SourceGrid.Cells.Controllers.ControllerContainer.OnMouseUp(CellContext sender, MouseEventArgs e)
       at SourceGrid.GridVirtual.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at swisstopo.geodesy.filemanager.Program.Main() in D:\PROJECTS\VS2005\FileEditor\VS.NET\FileManager\Program.cs:line 27
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


It's not so catastrophic, becasue I'll have to programm the sort function myself with these spanned rows and multiple header row, because in my case I need something like a "group" sorting, for each section separated with a header.

But anyway I think there's still one bug in the sorting algorithm with the last spanning management modifications.

Best Regards.

Jul 1, 2009 at 5:38 AM

Jray, could you please file a new issue for the this sorting exception? I would certainly wnat this to be fixed.

Jul 5, 2009 at 1:07 PM

OK, I think that I nearly understand how Autosize is working. I had problem with column headers and spanned columns. So if I use AutoSizeCells(Range) it works.

But I don't really understand what AutoSizeMode.MinimumSize means.

What I wanted is a solution that only extents the column width if it isn't large enough, but don't strechs the width. But I also want that the width can be streched manually. So the MinimalWidth property isn't ok for me.

So if I well understood, the MinimumSize isn't the solution, and I'll have to resize each colomn manually, using MeasureColumnWidth method and compare it with my minimal width and then set the new width only if MeasureColumnWidth < "myMinimalWidth".

Is it correct or does an automatic option exist ?