views:

316

answers:

2

Hello you all ,

Sorry if my question seems to simple .

Right now in the BoundField of my GridView some data display like "12/11/1381" that i wanna display like "1381/11/12" (opposite direction) .

I know i could change the direction via rtl and ltr in TemplateField. But because of some reason i can't convert my boundfield to templatefield.

I looked through all BoundField properties in order to change direction , but i found nothing.

I think that should be done in RowDataBound event of GridView , But i don't know how !?

What do you think at this case ?
Thanks in advance.

A: 

First make sure you have set HtmlEncode="False"

try adding this to the BoundField tag -

DataFormatString="{0:yyyy/MM/dd}"

here is a good article that should help you - How To Set a Date Format In GridView

Pavanred
+1  A: 

Ok, since it's a date which was input as string, there are at least 2 ways you can do it.

Method 1
Databind the string as a Date object instead of String. As I do not now the exact date format of the original date, dd/MM/yyyy or MM/dd/yyyy, you'll have to figure it out yourself.

If you can do that, just add this to the "BoundField" element:

DataFormatString="{0:yyyy/MM/dd}"

or

DataFormatString="{0:yyyy/dd/MM}"

Method 2
Manipulate the text in ROwDataBound oevent. I've done a crude one here which can be polished up. You should probably manipulate them as Date objects.

In the ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
 onrowdatabound="GridView1_RowDataBound">

CodeBehind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;
    // assuming the date is at cell index 1
    string[] arr = e.Row.Cells[1].Text.ToString().Split('/');
    e.Row.Cells[1].Text = string.Format("{0}/{1}/{2}", arr[2], arr[1], arr[0]);
}
o.k.w
Thank you . That definitely works in situation that i have datetime . But there are other cases that my boundfield are in string data type and not datetime . Any solution except converting to template field for those ?
Mostafa
@Mostafa: I do not quite understand your question. `RowDataBound` event allows you do perform any string operation, doesn't have to be `Date` type.
o.k.w
You are right . Thank you
Mostafa