Format Data in BoundColumns of DataGrid

R

ruca

Hi

I want to format some columns of my DataGrid control.
Example, one of my fields of DataSet is a DATE field and in BD he is Ok, but
when I show him in datagrid he comes with hours too. I don´t want that.

2nd Example:
Field in BD:
DtSegm
17-02-2004
Note: When I save to DB I format Date to put like above

In DataGrid shows like this:
DtSegm
17-02-2004 10:30:45

How can I change that.
 
J

Jose Marcenaro

If you are using simple bound columns (using the Property builder) you may
asign the Data Format box in the property builder, or manually assign a
DataFormatString attribute in HTML view

If its a templated column, you may add the format as the third argument to
the Eval function, as in:

DataBinder.Eval(Container, "DataItem.DueDate", "d")

Regards
Jose.
 
R

ruca

What's wrong with this:

<asp:BoundColumn DataField="DtSgm" ReadOnly="True" HeaderText="DtSegmento"
DataFormatString="DtSgm.ToString('dd-MM-YYYY')"></asp:BoundColumn>
 
M

Martin Dechev

Hi, ruca,

You haven't read the article for the DataFormatString property, have you?

<asp:BoundColumn DataField="DtSgm" ReadOnly="True" HeaderText="DtSegmento"
DataFormatString="dd-MM-yyyy"/>

Greetings
Martin
 
M

Martin Dechev

It should be "{0:dd-MM-yyyy}" in this case, sorry for the mistake.

Hope this helps
Martin
 
R

ruca

How can I format a Field that I have to calculate something.
Like this:
Field1 in DB is a Double
Field1 = 0.11

And I want to appear: 0.11 * 100 = 11


Thanks
 
R

ruca

I've got working now. I already understand how can I format string's in the
way that I want. Thank's Martin.

But now I have another question:

How can I format a Field that I have to calculate something.
Like this:
Field1 in DB is a Double
Field1 = 0.11

And I want to appear: 0.11 * 100 = 11


Thanks
 
J

Jose Marcenaro

1. In the grid's Property Builder page, clear the "Data Format" field and
then convert your "bound column" into a "templated column" using the
provided link

2. Edit the HTML and replace the binding expression as follows (assuming
DataItem.Qty is an integer)

Instead of
<%# DataBinder.Eval(Container, "DataItem.Qty") %>
use
<%# (((int)DataBinder.Eval(Container,
"DataItem.Qty"))*100).ToString("d9") %>

Regards
Jose.
 
R

ruca

<%# (((int)DataBinder.Eval(Container,"DataItem.Qty"))*100).ToString("d9") %>

The part of ToString("d9") doesn't work.
I have to set this value: DataBinder.Eval(Container,"DataItem.Qty"))*100)
into a double of 2 decimal places

How?
 
J

Jose Marcenaro

Use either .ToString("#.00")
or .ToString("#,###.00") if you want thousands separators
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top