Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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)
C#
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
C#
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
Updated 19-Jan-13 20:43pm
v3
Comments
CHill60 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 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.
[no name] 20-Jan-13 3:10am    
can you show your code to us?

 
Share this answer
 
Comments
NeueStudium2012 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.
Define the IComparable feature and create the comparison feature inside the class:

C#
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>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900