Dear All,
My database will return the time in the format of 24hrs as follows.
2012-04-10T21:30:00
But i need to show only timing in GridView as follows
Retrieving Format Required format
2012-04-10T21:30:00 9:30PM
2012-04-10T09:30:00 9:30AM
I need to extract only timing from this & i need to show in GridView as specified format.
How can i achieve this?
Loading

Jignesh TrivediPosted Apr 11, 2012, 3:49 AM
In this case RowDataBound event of grid view definately help you to resolve this issue.
Try.
First register RowDataBound event on page load
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
}
AND
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Date = e.Row.Cells[5].Text;
DateTime dt = new DateTime();
DateTime.TryParse(Date, out dt);
e.Row.Cells[5].Text = dt.ToShortTimeString();
}
}
In above code e.Row.Cells[5].Text Replce you index. i.e. "5" is replace with your actual index of column in above code.
Hope this help.
SIVAPosted Apr 11, 2012, 3:06 AM
Jignesh TrivediPosted Apr 11, 2012, 2:16 AM
are taking your webservice responce in datatable or dataset?
If yes then check the datatype of the column that contain date.
it might your column datatype is string so auto format is not support.
I am trying following code.
code side:
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn();
dc1.DataType = typeof(System.String);
dc1.ColumnName = "Name";
dt.Columns.Add(dc1);
DataColumn dc = new DataColumn();
dc.DataType = typeof(System.DateTime);
dc.ColumnName = "myDate";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = "Jignesh";
dr[1] = DateTime.Now;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Aspx side:
try and let me if you have any more query.
hope this help.
SIVAPosted Apr 11, 2012, 1:36 AM
Dear All,
I tried all these options..
my webservice will return Datetime from the database.
I need to customize & display it in Grid as specified above..
Thank you.
Jignesh TrivediPosted Apr 11, 2012, 12:16 AM
please try, following code in column tag.
hope this help.
SIVAPosted Apr 10, 2012, 1:52 PM
<%#Eval("MenuTimeFrom")%>
Is it possible to customize Here?SenthilkumarPosted Apr 10, 2012, 10:01 AM
You can convert the timing as below.
DateTime dt = DateTime.Now;
String.Format("{0:t}", dt);
This will return exactly what you expect...
DateTime.Now.ToString("hh:mm tt")