Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Custom sort of DataGridView

Tags:

I need to sort a DataGridView with Natural Sorting (Like in Explorer) so that numbers and text (in the same column) are sorted naturally, and not alphabetically (so that "place 3" comes before "place 20", etc.). I have a DataGridView, where I have set a DataView as DataSource. The DataView contains a DataTable which is created with some values from a database. The column types are string. I have an IComparer, which does what it should, but I can't figure out how to use it, cause I can't find out how to do the sorting. The DataGridView.SortCompare event, which would be perfect, doesn't work since it is databound. The DataView.Sort, only accept strings with column names and sort orders.

Very annoying. Tried to read related issues here on StackOverflow, and searched google lots and lots, but I can't really find much about this. Only stuff I really find is using that Sort(string) method of the dataview, which wont work, since it sorts alphabetically.

Does anyone know how to do this without too much trouble? It got to be others than me struggeling with this? I really don't want to re-implement the whole datagridview or dataview classes, just to get custom sorting...

Update: In case someone were wondering, I'm still looking for a good answer to this problem. Although in the mean time, I ended up creating my own simple table class, and then feed that into a datagridview manually. Overriding the SortCompare method. Bit annoying, but wasn't too hard, since I only need to show values (no editing or anything) and therefore could convert everything to strings.

like image 361
Svish Avatar asked Jan 12 '09 11:01

Svish


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

Take a look at this MSDN page and this blog post. In principle, you need to configure the sorting at the data source (whether its an ObjectDataSource or a SqlDataSource) not at the GridView.

As far as I can tell the DataView class doesn't support anything other than a simple ascending/decending sort. Without seeing the code where you load and bind the data it's hard to make a specific recommendation, but you could either:

  1. Load your data into a List instead of a DataTable, call the Sort method passing in your comparison method and then bind to that list.
  2. Create an ObjectDataSource in your aspx code that gets the data directly from a class, and configure that ObjectDataSource to use your IComparer.
like image 172
d4nt Avatar answered Sep 19 '22 21:09

d4nt