Formatting date in Gridview?

Posted by Laghaterohan under Regular Expressions on 2/3/2011 | Points: 10 | Views : 9241 | Status : [Member] | Replies : 10
Hello,

Can any one please let me know the exact solutions as to how to display a date in a desired format in a grid view control?

I searched for many articles but none of the solutions seem to work.
I have tried it by taking a Template field and then inside a item template displaying the the date in the desired format but none seems to work.

To tell u my datatype in DB for storing date is Varchar and i save date in 2011-02-04 Format. But while displaying it, i wish to display it in 04-Feb-2011 format..Can any one please let me know how can i do this??


Thanks!

Best Regards,
Rohan Laghate



Responses

Posted by: Vuyiswamb on: 2/4/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
this should do
http://www.codedigest.com/Articles/ASPNET/137_How_to_format_DateTime_in_GridView_BoundColumn_and_TemplateColumn.aspx

Thank you for posting at Dotnetfunda
[Administrator]

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhuk on: 2/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Rohan using date as Varchar is bad practice always use date as datetime datatype. I suggest you to change to datetime if it is possible.

For your requirement
protected string Getdatestring(object date)

{
string srr = date.ToString();
// string srr = "2011-02-04";
string[] spl = srr.Split('-');
string newdt = spl[1] + "-" + spl[2] + "-" + spl[0];
DateTime dt1234 = Convert.ToDateTime(newdt);
string strqwe = string.Format("dd-M-yyyy", dt1234);
}

In itemtemplate of template field
<%# Getdatestring(Eval("datefiels")) %>



Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 2/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hello,

I tried all the formats given by Vuyiswamb but none of them seems to work for me.

I think the problem is with my datatype in the database. Its varchar and that is giving me the problem...

Thanks!


Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhuk on: 2/4/2011 [Member] Starter | Points: 25

Up
0
Down
@Rohan
Yes the link given by Vuyiswamb works only if the datatype is datefield.
Did you try my answer I think that should work for your requirement.

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/4/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Yes, Either you convert the data type as DateTime or if you cannot then try this it works,

in the row databound event

if(e.Row.RowType == DataControlRowType.DataRow)
{
//Here you can set the cell number 0 or 1 based on the position
e.row.cells[0].Text = Convert.ToDateTime(e.Row.Cells[0].Text).ToString("dd-MMM-yyyy");
}



Thanks and Regards
Madhu

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/4/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Simple thing to do is you can convert it in the SQL stored procedure itself before you come to front end and since its a string you can directly map to the gridview.

Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/4/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Laghaterohan

U SET Gridview date column DataFormatString {0:d} or {0:c} then u display your desired format

hope fully it's working for u ..

Best Regard's
Prabhakar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 2/4/2011 [Member] Starter | Points: 25

Up
0
Down
@Madhuk Thanks for the solutions..i shall try it and let you know..
@Karthikanbarasan : Ok..
@ Madhu.b.rokkam : Shall try and let u know....


Thanks!

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhanu55prakash on: 9/1/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
In the asp: BoundField u need to write
DataFormatString="{0:d}" To Display Date Only or
DataFormatString={"0:dd/MM/yyyy"} to change format

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jasminpatel on: 9/2/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

To tell u my datatype in DB for storing date is Varchar and i save date in 2011-02-04 Format. But while displaying it, i wish to display it in 04-Feb-2011 format..

When You fetch data from database at that time you cat fetch you DATE as you required and Write Query like.

SELECCT Convert(varchar(10),Database_ColumenName,(106)) as DATE Form Table
See Bellow Link For Date and Time Conversion.
http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/

Thank You.

jasmin patel

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response