views:

269

answers:

3

I found a few questions similar to this one here on SO, but none that matched this problem, so here we go.

I've got a DataGridView showing members of a team. All the team members have an assigned role within the team listed in one of the columns. Examples could something like be "Legal Representative", "Account Manager", "Assistant Account Manager" or "Accountant".

Now here's where it gets interesting. I basically want to sort the grid on this column alphabetically, with a couple of exceptions. The "Account Manager" should always be listed at the top, followed by the "Assistant Account Manager" if there is one.

The objects and grid are all operational at this point, and have been in production release for some time, so I don't want to do more work on this than strictly necessary.

Is there an easy way to do this? I assume I have to do it programatically...

Some pseudo-code to clarify:

if (memberRole == 'Account Manager') 
{
    //put in top row
}
else if (memberRole == 'Assistant Account Manager')
{
    //put in second row 
}
else
{
    //sort remaining rows alphabetically
}

I do my work in C# .NET using Visual Studio 2008.

+2  A: 

You can capture the SortCompare event of your datagridview:

    private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        string cell1, cell2;

        if (e.Column == "Your_Column")
        {
            if (e.CellValue1 == null) cell1 = "";
            else cell1 = e.CellValue1.ToString();

            if (e.CellValue2 == null) cell2 = "";
            else cell2 = e.CellValue2.ToString();

            if (cell1 == "Account Manager") { e.SortResult = -1; e.Handled = true; }
            else
            {
                if (cell2 == "Account Manager") { e.SortResult = 1; e.Handled = true; }
                else
                {
                    if (cell1 == "Assistant Account Manager") { e.SortResult = -1; e.Handled = true; }
                    else
                    {
                        if (cell2 == "Assistant Account Manager") { e.SortResult = 1; e.Handled = true; }
                    }
                }
            }
        }
    }
najmeddine
A: 

Or while getting the data from server side you can sort it and then pass it to the front end via w/e json, etc etc so all the list does is show the data, and the data was sorted on the server

Faisal Abid
I'm a little unsure of what you are suggesting here. If you are suggesting I implement something on the server side that is not an option, unfortunately. Server side solutions are rock solid non-changeable solutions (made in KOBOL like 20 years ago) that we have to work with as they are.
Sakkle
A: 

An other solution that may not appeal to some but is fast to implement and works well is to introduce a new property on the object for sorting purposes. Make the new property contain a sorting character as the first character (a number works well) and the actual sorting value as the rest of the characters. Implement some easy if-else statements to set the appropriate value of the sorting property.

When adding this column to the grid just make it hidden and sort on that column.

Possibly a less elegant solution than the one proposed by najmeddine, but it works.

Sakkle