Resolved "Column provided does not belong to this DataGridView control."

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,800
Location
Norway
Programming Experience
10+
I have a DataGridView bound to a SortableBindingList(Of T), written based on Custom Data Binding, Part 2. When I set DataSource and call Sort (done in BindDGVs method) I get the exception message "Column provided does not belong to this DataGridView control.".
This is the call that throws:
VB.NET:
Me.FilterGrid.Sort(Me.colFilterType, System.ComponentModel.ListSortDirection.Ascending)
This is stacktrace:
System.ArgumentException was unhandled by user code
Message="Column provided does not belong to this DataGridView control."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
at RSSview.Form1.BindDGVs() in D:\programming\vbnet35\RSSview\RSSview\Form1.vb:line 218
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
InnerException:
If I change call to this the Sort is performed without error:
VB.NET:
Me.FilterGrid.Sort(Me.FilterGrid.Columns("colFilterType"), System.ComponentModel.ListSortDirection.Ascending)
The grid columns configured in Designer has different AutoSizeMode set, AllCells for the column sorted on. From what I can tell from online searches for the error message AutoSizeMode can be problematic, though it doesn't seem confirmed nor resolved.

If it matters, data loading is done asynchronous from Forms Load event, when done loading BindDGVs is invoked to UI thread, this call always happens after form is Shown.

Anyway I have it resolved, but posted for others that may encounter it, or if someone has some input to this.
 
Well, it helps to put it out. I did some more debugging (the problem above is actually an old one I came across again today) and the cause of this exception is now clear. Comparing the column objects reveals these two references are not the same after binding, I had all columns configured and didn't think about AutoGenerateColumns, which is True by default. What happens then is that the grid clones existing matches and generates missing ones, causing all columns in grid to be new objects. The column referenced by Designer generated column variable will then be left an orphaned object no longer attached to the grid. So if you use Designer and don't need some columns auto-generated when binding remember to set AutoGenerateColumns=False.
 
Back
Top