2

I have a wpf datagrid. I assign ObservableCollection to it.

DG1.DataContext = a; 

One of the columns is having values like following

1_A_B
12_A1_B
3_A2_B
10_A3_B
2_A4_B
15_A5_B

i want to sort the datagrid using the first integer value like following

1_A_B
2_A4_B
3_A2_B
10_A3_B
12_A1_B
15_A5_B

If i sort using this column, it is taking as string ascending like following(which is not i want)

1_A_B
10_A3_B
12_A1_B
15_A5_B
2_A4_B
3_A2_B

I want to sort using the first integer value in above column

2
  • Do you AutoGenerateColumns? Can you add another column to view model of the row that would give you first part of that value as int? Commented Mar 26, 2014 at 12:28
  • i use AutoGenerateColumns=false. i dont want to add a new column to datagrid or ObservableCollection<T>. Commented Mar 26, 2014 at 12:36

1 Answer 1

4

The values 1_A_B, 12_A1_B etc will be contained within the property bound to that column. Let's call this PropertyA.

Possibly the easiest way to achieve this is to have another property on the data object, let's call that YourSortOrder. When the setter on PropertyA gets called with a non null value, you can use simple string manipulation together with an int.Parse() to extract the numeric value and assign it to YourSortOrder.

This code isn't production quality but illustrates the point:

public string PropertyA
{
    get { ... }
    set
    {
        _propertyA = value;
        if (value != null)
            YourSortOrder = int.Parse(value.Substring(0, value.IndexOf("_", StringComparison.InvariantCultureIgnoreCase)));
    }
}

then set the SortMemberPath of your DataGridColumn to the property YourSortOrder:

<DataGridColumn x:Name="xxxxx"
                SortMemberPath="YourSortOrder" 
                ...etc...
Sign up to request clarification or add additional context in comments.

4 Comments

i thought of this idea already.The problem which i suspect is performance. I have 6 columns in datagrid which have huge data. And there are lakhs of record in the datagrid. The time taken to load the datagrid is huge already. If we add another int property, that will bring down the performance. Please correct me if i am wrong
@senthilraja This particular property won't slow down the grid in any appreciable way, but you will take a tiny hit every time you need to set that property. If you could denormalise your data a little bit and have that int value precalculated in the database you would get the best performance for it.
@senthilraja Also ensure you are using a grid that has virtualization turned on (so UI elements only get generated for what is visible and what is just outside the viewport). You should be able to bind literally millions of records to a grid - but there can be things you need to look out for like binding errors, heavy use of converters, excessively wide datarows, etc.
i tested with my huge data. it works. thanks Slugster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.