intercept sorting of a databound datagridview

Posted on 2009-04-17
Last Modified: 2013-12-17
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
Question by:Arikael
    LVL 3

    Author Comment

    LVL 3

    Expert Comment

    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.
    LVL 3

    Author Comment


    a datagridview.

    with sorting event, you mean the SortCompare event?
    LVL 3

    Accepted Solution


    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.
    DataView dv = new DataView(dataGridView1.DataSource as DataTable);
    dv.Sort =  string.Format("{0} {1}, {2} {3}", "YourColumn", "ASC",   
               (dataGridView1.SortOrder==SortOrder.Ascending)? "ASC":"DESC");
    dataGridView1.DataSource = dv.ToTable();

    Open in new window

    LVL 3

    Author Closing Comment


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now