• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

intercept sorting of a databound datagridview

Hi experts

I have a databound datagridview which can be sorted (columnheader click)
But everytime the dvg respectively the bindingsource behind it gets sorted because a user clicked on a columnhead, the dvg should first be sorted by a field I defined (always) and then by the field the user wants.

How can I achieve this?
is it possible to intercept the sorting and prefix the sort-string with my field?

I'm using c#, .NET 2.0 and WinForms
0
Arikael
Asked:
Arikael
  • 3
  • 2
1 Solution
 
ArikaelAuthor Commented:
Anybody?
0
 
BalkisBrCommented:
You're trying to sort a gridview with two columns rigth?

You can pass, on your sort expression, two or more columns separated by commas, like this:
"Col1 ASC, Col2 DESC"

If  it doesn't solve your problens, you can post your Sorting event code and i'll try to help you.
0
 
ArikaelAuthor Commented:
Hi

a datagridview.

with sorting event, you mean the SortCompare event?
0
 
BalkisBrCommented:
No,

I was referring to your code for arrange the sorting.

I believe that the easiest way to do what you want is to extract a DataView from your DataSource and then implement the sorting (see code).

You'll have to rearrange the SortOrder part because DataGrid.SortOrder value is not meaningful when using custom sorting. See link for reference.
 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortorder.aspx
DataView dv = new DataView(dataGridView1.DataSource as DataTable);
dv.Sort =  string.Format("{0} {1}, {2} {3}", "YourColumn", "ASC",   
           dataGridView1.SortedColumn.Name, 
           (dataGridView1.SortOrder==SortOrder.Ascending)? "ASC":"DESC");
dataGridView1.DataSource = dv.ToTable();

Open in new window

0
 
ArikaelAuthor Commented:
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now