Click here to Skip to main content
11,428,876 members (64,516 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I want to order the data in a grid based on a column contents.

I have tryed the instruction (OperationGrid is my grid's name, and "Part Number" the title of the first column)
 OperationGrid.Sort(OperationGrid.Columns["Part Number"], ListSortDirection.Ascending);
In that case, I get the error
"Value cannot be null.
Parameter name: dataGridViewColumn
"

Any idea of where can the error come from?

and also
OperationGrid.Sort(OperationGrid.Columns[0], ListSortDirection.Ascending);
Now I get the error "DataGridView control must be bound to an IBindingList object to be sorted".

Any idea of where can the error come from?
Maybe I forgot something to define? but the grid works fine without sorting.


I believe that the problem is that I have forgotten something to declare or a type can be wrong. Any idea about that?


Thank you in advance.
David
Posted 17-Jan-13 0:04am
Edited 19-Jan-13 21:43pm
v3
Comments
CHill60 at 17-Jan-13 6:19am
   
Your first error "Value cannot be null. Parameter name: dataGridViewColumn" could be a misspelling of the column name ... e.g. is it PartNumber or Part number. I can't reproduce the 2nd error which is why I'm posting this as a comment rather than a solution
NeueStudium2012 at 17-Jan-13 10:15am
   
Thank you for the suggestion but it is not a misspelling, as I copied the value direct from the configuration file. I also used the array index that refers to this column (like col[0].Title) with the same results.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
NeueStudium2012 at 20-Jan-13 2:44am
   
I would like to avoid sort it programmatically. It is a lot of work and as the sorting method already exists, it must be something that I left to do, but I do not guess what.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Define the IComparable feature and create the comparison feature inside the class:

public class Component : IComparable<component>
{
   private string PartNumber;
 
...
 
   public int CompareTo(Component toSort)
   {
      return PartNumber.CompareTo(toSort.PartNumber)
   }
 
...
 
}
</component>

Usage:

private void ListSort(list<component> _list2Sort)
{
   _list2Sort.Sort();
}
 
</component>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100