We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sorting not working for columns that are bound to collection items

I want to bind a column to a value at a particular index in a List or a particular key in a Dictionary.  The values display properly in the grid however when I attempt to sort by that particular column the grid stops responding.  I pasted some sample code that reproduces the problem.  This grid will build and display correctly but once sorted by either the C3 or C4 column it will not sort and also clicking on the C1 and C2 column headers will no longer work.

class TestData
{
public string C1 { get; set; }
public decimal C2 { get; set; }
public List<int> C3 { get; set; }
public Dictionary<string, int> C4 { get; set; }
}

void BuildGrid(SfDataGrid grid)
{
     grid.AllowSorting = true;

grid.Columns.Add(new GridTextColumn()
{
HeaderText = "C1",
MappingName = "C1",
AllowSorting = true,
AllowResizing = true
});

grid.Columns.Add(new GridNumericColumn()
{
HeaderText = "C2",
MappingName = "C2",
AllowSorting = true,
AllowResizing = true
});

grid.Columns.Add(new GridNumericColumn()
{
HeaderText = "C3",
MappingName = "C3[0]",
AllowSorting = true,
AllowResizing = true
});

grid.Columns.Add(new GridNumericColumn()
{
HeaderText = "C4",
MappingName = "C4[C4]",
AllowSorting = true,
AllowResizing = true
});

var data = new List<TestData>
{
new TestData 
C1 = "1", 
C2 = 1M, 
C3 = new List<int> { 1, 11, 111 }, 
C4 = new Dictionary<string, int> { { "C4", 1 } }
},
new TestData 
C1 = "2", 
C2 = 2M, 
C3 = new List<int> { 2, 22, 222 }, 
C4 = new Dictionary<string, int> { { "C4", 2 } }
}
};
grid.ItemsSource = data;
}


Is there anything that I am missing here that might be causing the grid to behave this way?

Thanks


3 Replies

SV Srinivasan Vasu Syncfusion Team June 26, 2017 06:55 AM UTC

Hi Ryan, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have prepared a sample as per your provided details. If the data operations are not working as expected while using indexer or dictionary property in the Underlying Collection, set GridColumn.UseBindingValue as true. 
 
Please refer the below code  
 
   this.datagrid.Columns.Add(new GridNumericColumn() 
            { 
                HeaderText = "C3", 
                MappingName = "C3[2]", 
                AllowSorting = true, 
                AllowResizing = true, 
                UseBindingValue = true, 
            }); 
 
  
 
Please refer the below UG Link: 
 
 
Regards, 
Srinivasan 



RL Ryan Lim June 26, 2017 05:58 PM UTC

That did the trick.  I took the following code in my sample:

grid.Columns.Add(new GridNumericColumn()
{
     HeaderText = "C3",
     MappingName = "C3[0]",
     AllowSorting = true,
     AllowResizing = true
});

I then just made a single line change in the code to:

grid.Columns.Add(new GridNumericColumn()
{
     HeaderText = "C3",
     MappingName = "C3[0]",
     AllowSorting = true,
     AllowResizing = true,
     UseBindingValue = true
});

Sorting now works.  Thanks for the quick response!



MK Muthukumar Kalyanasundaram Syncfusion Team June 27, 2017 09:24 AM UTC

Hi Ryan, 
 
Thanks for the update. Please let us know if you need any other assistance. 
 
Regards, 
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon