This is a migrated thread and some comments may be shown as answers.

Sorting on Column Formatted in RowFormatting

1 Answer 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hugo Furth
Top achievements
Rank 1
Hugo Furth asked on 28 May 2015, 12:46 AM

Hi All -

I have the following bit of code which works fine....I'm formatting a column based on the data in another column.

The problem I have arises when I try to sort on 'Display1' column by clicking the header. If I try to do when the grid is first populated, it doesn't sort. If I've scrolled thru the data once, then everything is fine.

I understand why this happens: Since the RowFormatting method is not called until the row in question is displayed, the 'Display1' values have not yet been populated in my non-visible rows.

So my question is this: What is the best way to get 'Display1' to sort properly? Do I simulate scrolling thru the grid to force RowFormatting to do its work? Or is there a better way?

Thanks as always,

Hugh

      

private void MasterTemplate_RowFormatting(object sender, RowFormattingEventArgs e)
    {           
    if (e.RowElement.RowInfo.Cells["Data1"] != null)
        {
        if ((String)e.RowElement.RowInfo.Cells["Data1"].Value == "")
            e.RowElement.RowInfo.Cells["Display1"].Value = "";
        else
            e.RowElement.RowInfo.Cells["Display1"].Value =
                                FormatData(e.RowElement.RowInfo.Cells["Data1"].Value);                   
        }
    }

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 28 May 2015, 03:11 PM
Hello Hugo,

Thank you for writing.

The formatting events are not suitable for setting the value. Setting the value can trigger the event again and this can lead to an endless loop. These events should be used for setting visual styles or the text, but no the actual cell value. Detailed information can be found in the following articles:
In your case it would be better to iterate all cells after the grid is bound and set their value. For example this can be done in the DataBindingComplete event. 

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Hugo Furth
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or