DataGridView custom sort not on DataMember (Databound Grid)

I have a custom DataGridView column that uses an embedded control that pops up a search window for the value of that column. The important thing is that the databound column is a numeric ID, but the custom column cells display a text description.

How do I get the column to sort on the text description rather than the numeric ID?

I don't see a way to override the column to sort by FormattedValue instead of Value. I could ensure that the description shows up as a separate column in my data table, but I don't see any way to say "use column VALUE_ID as DataMember but column VALUE_DESCRIPITON as 'SortMember'"

Answers


You can use a technique described in the following article Column Sort Modes

private bool ascending;
private int sortColumn;
private void dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    List<SomeObject> list = (List<SomeObject>)someBindingSource.DataSource;
    if (e.ColumnIndex != sortColumn) ascending = false;

    int 1 = e.ColumnIndex;
    if (i == DescriptionColumn.Index)
        list.Sort(new Comparison<SomeObject>((x,y) => x.ID.CompareTo(y.ID)));

    sortColumn = e.ColumnIndex;
    ascending = !ascending;
    if (!ascending) list.Reverse():

    someBindingSource.ResetBindings(false);
    // you may also have to call dgv.Invalidate();
}

Need Your Help

Checkboxes/Radio Buttons- Adding price total of chosen item

javascript

I am currently working with radio buttons and checkboxes. In this example project I have radio buttons and checkboxes displaying an image based on the value that is chosen by a user.

c#: how to get the object that inherited the passed in object

c# oop object inheritance

I am making a game/game engine and encountered the following problem: i have a player that has a "checkCollision(Wall w)" method. it takes in a very simple object wall, which has some data and chec...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.