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

CellFormatting problem

2 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alcide Burato
Top achievements
Rank 1
Alcide Burato asked on 16 May 2011, 09:44 AM
Hello,

I have a problem with CellFormatting handle of GridView.
I have a column that contains 8-char strings that rappresents a date in the format yyyyMMdd.
In order to make more confortable the visualization of the date to the user, I change the format in dd/MM/yyyy using CellFormatting.

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
   base.OnCellFormatting(sender, e);
   GridCellElement ce = e.CellElement;
   ce.Value = ChangeDateFormat(ce.Value);
}

The problem I got in runtime is it seems the GridView apply this cell formatting only for the cells that are currently displayed to the user.
The other cells that are not visible but could be reach using the vertical scrollbar  mantains the old format yyyyMMdd.

As a terrible consequence of this behavior is when I group by the date column, I have some rows with the original format yyyyMMdd, and other rows with the new format dd/MM/yyyy. Although they rappresents the same date (such as 20111012 and 12/10/2011) the grouping engine recognize them as different values and it groups the rows in two different groups.

So I need a suggestion to avoid this problem, what should be the correct method to handle to makes this cell formatting in order to be sure it is done to all the rows.

Another conseguence of the problem is when I export to excel or cvs, the behaviour is pretty like the same: I got some rows with the new format dd/MM/yyyy (exacly the rows that are currently displayed) and the other are saved with the old format (yyyyMMdd).

I suppose this is a sort of mechanism where the cell formatting is applied in runtime only to the displayed cells, probably for speed optimization purpose. In this case for me the cell formatting method is totally unuseless, so I need to know the right way to do the job.

Thanks for any aid
Alcide

2 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 16 May 2011, 12:45 PM
Hello Alcide,

From what i can see here, the problem is that you are changing the actual value of the cell, instead of changing just the displayed text, please try something like this:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column.Name == "Date" && e.CellElement.Value != null)
    {
        e.CellElement.Text = GetFormattedStringDate(e.CellElement.Value.ToString());
    }
}

And one more think, i guess you know this already but if you want to use a custom format header row please take a look at this help article: Formatting Group Header Row.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Alcide Burato
Top achievements
Rank 1
answered on 17 May 2011, 10:58 AM
Hello Emanuel
thanks for the answer.

I solved the problem changing both Text and Value properties of the cell in CellFormatting method and changing the Value in GroupSummaryEvaluate for the cells that are type of GridViewGroupSummaryItem.

This is because if I change only the Text property when I group by the date, the date value shown in group header row remains in yyyyMMdd format.

Regarding the exporting in csv and html I solved handling the CSVCellFormatting and HTMLCellFormatting methods because like the grouping process the are based on the Value property of the cell and not the Text.

Thanks for the aid,
Alcide
Tags
GridView
Asked by
Alcide Burato
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alcide Burato
Top achievements
Rank 1
Share this question
or