GridView DataFormatString at RunTime

B

bsm

I have created GridView and assign Datasource at RunTime. I need to
format one Column which will have the following data.

Start_date Want to Format as
Start_date
--------------
------------------------------------------
10-Mar-2005 10:50:00 10-03-2005

Discard Time and change the Date Format.

Please anyone tell me how to achieve this.
 
B

bsm

yourDate.ToString("MM-dd-yyyy");

Ray at work









- Show quoted text -

Let me explain my problem more clearly.

1. I drag and drop GridView in Design time.
2. In run time I assigned the Datasource and Bind it.
3. The gridview contains 4 columns, one of them is DataTime.
4. Which will display the data like "10-Mar-2005 10:50:00".
5. But our user don't like to see the Time in the Gridview report.
6. I need to eliminate the Time during the RunTime (or if u have any
idea, plz suggest).
7. What I did was, in the prerender event I tried to set the
"DATAFORMATSTRING" property of the column.
8. But it throws error.

Hope I explained my issue step by step.
Plz guide me what to do?
 
R

Ray Costanzo

Set the dataformat string to "MM-dd-yyyy"

Example:

ASPX:
<asp:DataGrid ID="dg" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="dt" DataFormatString="{0:MM-dd-yyyy}"
HeaderText="Formatted Version" />
<asp:BoundColumn DataField="dt" HeaderText="Unformatted Version" />
</Columns>
</asp:DataGrid>


CS: (binds some bogus data)

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("dt", Type.GetType("System.DateTime"));
for (int q = 1; q < 11; q++)
{
DataRow r = dt.NewRow();
r["dt"] = System.DateTime.Now.AddDays(q);
dt.Rows.Add(r);
}
dg.DataSource = dt;
dg.DataBind();
}

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top