By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Gridview Item formatting

P: n/a
I have a gridview control. In this grid view there is a column for the
price of an item. Some of the prices have a dollar sign in front of it and
some dont. How do I get rid of the dollar sign if it is in front of the
value? My guess would be to use a template column but I dont know how to go
about this. Any ideas?

Below is an example of what the data looks like raw from the database:

$456.95
200.89
34.95
$123.90

This is how I would like the data to appear in the gridview:

456.95
200.89
34.95
123.90

Hope this helps.
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Washoetech,

Welcome to ASPNET newsgroup.
From your description, you're using the ASP.NET 2.0's GridView control to
display some data, and one of the data fields will contains price value
which may or may not have a $ prefix, you're wondering how to remove this $
if exist through the Gridview's databinding interface, yes?

As for this question, I think we have the following two simple means:

1. Use BoundField, and intercept the GridView's RowDataBound event, in the
RowDataBound event, we can get the Binded Data from the certain GridView
Row's Cell and modify it (remove the '$') if necessary, for example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = "$" + e.Row.Cells[2].Text;
}
}

in this example, we modify the already binded data by appending a "$" , you
can use your own code logic to adjust it. e.Row.Cells[2] is just used to
find the correct Cell corresponding to the BoundField column.

2. We can also use TemplateField in which we can define our own control
template and use own custom helper functions to format the binding data ,
for example, the following is a simple template field which use a custom
function(defined in page class) which modify the original binding data
retrieved from datasource:

<asp:TemplateField HeaderText="TemplatePrice">
<ItemTemplate>
<%# AddDollar(Eval("UnitPrice").ToString()) %>
</ItemTemplate>
</asp:TemplateField>

AddDolloar is the helper function defined in page class:

protected string AddDollar(string ostr)
{
return "$" + ostr;
}
Both means are very simple. If you have anything unclear, please feel free
to post here.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "washoetech" <wa********@newsgroups.nospam>
| Subject: Gridview Item formatting
| Date: Wed, 28 Sep 2005 17:19:34 -0700
| Lines: 23
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <#l**************@TK2MSFTNGP14.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 206.159.118.137
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:127830
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I have a gridview control. In this grid view there is a column for the
| price of an item. Some of the prices have a dollar sign in front of it
and
| some dont. How do I get rid of the dollar sign if it is in front of the
| value? My guess would be to use a template column but I dont know how to
go
| about this. Any ideas?
|
| Below is an example of what the data looks like raw from the database:
|
| $456.95
| 200.89
| 34.95
| $123.90
|
| This is how I would like the data to appear in the gridview:
|
| 456.95
| 200.89
| 34.95
| 123.90
|
| Hope this helps.
|
|
|

Nov 19 '05 #2

P: n/a
Steven,

In your second example it basically says that I can create a function and
then call it from within the template. How easy. All this time I thought
that it was so hard. The answer was right in front of my face.

After I originally posted this message I tweaked with the code and came up
with this:

<ItemTemplate>

<asp:Label runat="server" Text='<%# "$" & Eval("jobber").Replace("$",
"").Trim() %>' />

</ItemTemplate>

What this does is pulls the data and then removes any instances of a dollar
sign and then trims any white space from the string. Then it adds a dollar
sign to every item. This works perfect for what I need. However, your
second example gives me much more flexability and will help with my second
problem which is adding a percentage modifier to the number. In other words
the number is my customers wholesale amount and I created a configurable
modifier that adds a percentage to that amount so that their customer is
paying the correct price not the wholesale price. In this instance a
function will help immensely.

I really appreciate your help! Thanks.

washoetech
"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:Iw*************@TK2MSFTNGXA01.phx.gbl...
Hi Washoetech,

Welcome to ASPNET newsgroup.
From your description, you're using the ASP.NET 2.0's GridView control to
display some data, and one of the data fields will contains price value
which may or may not have a $ prefix, you're wondering how to remove this
$
if exist through the Gridview's databinding interface, yes?

As for this question, I think we have the following two simple means:

1. Use BoundField, and intercept the GridView's RowDataBound event, in the
RowDataBound event, we can get the Binded Data from the certain GridView
Row's Cell and modify it (remove the '$') if necessary, for example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = "$" + e.Row.Cells[2].Text;
}
}

in this example, we modify the already binded data by appending a "$" ,
you
can use your own code logic to adjust it. e.Row.Cells[2] is just used to
find the correct Cell corresponding to the BoundField column.

2. We can also use TemplateField in which we can define our own control
template and use own custom helper functions to format the binding data ,
for example, the following is a simple template field which use a custom
function(defined in page class) which modify the original binding data
retrieved from datasource:

<asp:TemplateField HeaderText="TemplatePrice">
<ItemTemplate>
<%# AddDollar(Eval("UnitPrice").ToString()) %>
</ItemTemplate>
</asp:TemplateField>

AddDolloar is the helper function defined in page class:

protected string AddDollar(string ostr)
{
return "$" + ostr;
}
Both means are very simple. If you have anything unclear, please feel
free
to post here.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "washoetech" <wa********@newsgroups.nospam>
| Subject: Gridview Item formatting
| Date: Wed, 28 Sep 2005 17:19:34 -0700
| Lines: 23
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <#l**************@TK2MSFTNGP14.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 206.159.118.137
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:127830
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I have a gridview control. In this grid view there is a column for the
| price of an item. Some of the prices have a dollar sign in front of it
and
| some dont. How do I get rid of the dollar sign if it is in front of the
| value? My guess would be to use a template column but I dont know how
to
go
| about this. Any ideas?
|
| Below is an example of what the data looks like raw from the database:
|
| $456.95
| 200.89
| 34.95
| $123.90
|
| This is how I would like the data to appear in the gridview:
|
| 456.95
| 200.89
| 34.95
| 123.90
|
| Hope this helps.
|
|
|

Nov 19 '05 #3

P: n/a
You're welcome Washoetech,

Have a good weekend!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "washoetech" <wa********@newsgroups.nospam>
| References: <#l**************@TK2MSFTNGP14.phx.gbl>
<Iw*************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Gridview Item formatting
| Date: Thu, 29 Sep 2005 12:48:22 -0700
| Lines: 145
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <us**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 206.159.118.137
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:128084
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Steven,
|
| In your second example it basically says that I can create a function and
| then call it from within the template. How easy. All this time I
thought
| that it was so hard. The answer was right in front of my face.
|
| After I originally posted this message I tweaked with the code and came
up
| with this:
|
| <ItemTemplate>
|
| <asp:Label runat="server" Text='<%# "$" & Eval("jobber").Replace("$",
| "").Trim() %>' />
|
| </ItemTemplate>
|
| What this does is pulls the data and then removes any instances of a
dollar
| sign and then trims any white space from the string. Then it adds a
dollar
| sign to every item. This works perfect for what I need. However, your
| second example gives me much more flexability and will help with my
second
| problem which is adding a percentage modifier to the number. In other
words
| the number is my customers wholesale amount and I created a configurable
| modifier that adds a percentage to that amount so that their customer is
| paying the correct price not the wholesale price. In this instance a
| function will help immensely.
|
| I really appreciate your help! Thanks.
|
| washoetech
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:Iw*************@TK2MSFTNGXA01.phx.gbl...
| > Hi Washoetech,
| >
| > Welcome to ASPNET newsgroup.
| > From your description, you're using the ASP.NET 2.0's GridView control
to
| > display some data, and one of the data fields will contains price value
| > which may or may not have a $ prefix, you're wondering how to remove
this
| > $
| > if exist through the Gridview's databinding interface, yes?
| >
| > As for this question, I think we have the following two simple means:
| >
| > 1. Use BoundField, and intercept the GridView's RowDataBound event, in
the
| > RowDataBound event, we can get the Binded Data from the certain GridView
| > Row's Cell and modify it (remove the '$') if necessary, for example:
| >
| > protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs
| > e)
| > {
| > if (e.Row.RowType == DataControlRowType.DataRow)
| > {
| > e.Row.Cells[2].Text = "$" + e.Row.Cells[2].Text;
| > }
| > }
| >
| > in this example, we modify the already binded data by appending a "$" ,
| > you
| > can use your own code logic to adjust it. e.Row.Cells[2] is just used
to
| > find the correct Cell corresponding to the BoundField column.
| >
| > 2. We can also use TemplateField in which we can define our own control
| > template and use own custom helper functions to format the binding data
,
| > for example, the following is a simple template field which use a custom
| > function(defined in page class) which modify the original binding data
| > retrieved from datasource:
| >
| > <asp:TemplateField HeaderText="TemplatePrice">
| > <ItemTemplate>
| > <%# AddDollar(Eval("UnitPrice").ToString()) %>
| > </ItemTemplate>
| > </asp:TemplateField>
| >
| > AddDolloar is the helper function defined in page class:
| >
| > protected string AddDollar(string ostr)
| > {
| > return "$" + ostr;
| > }
| >
| >
| > Both means are very simple. If you have anything unclear, please feel
| > free
| > to post here.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "washoetech" <wa********@newsgroups.nospam>
| > | Subject: Gridview Item formatting
| > | Date: Wed, 28 Sep 2005 17:19:34 -0700
| > | Lines: 23
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | Message-ID: <#l**************@TK2MSFTNGP14.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: 206.159.118.137
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:127830
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I have a gridview control. In this grid view there is a column for
the
| > | price of an item. Some of the prices have a dollar sign in front of
it
| > and
| > | some dont. How do I get rid of the dollar sign if it is in front of
the
| > | value? My guess would be to use a template column but I dont know
how
| > to
| > go
| > | about this. Any ideas?
| > |
| > | Below is an example of what the data looks like raw from the database:
| > |
| > | $456.95
| > | 200.89
| > | 34.95
| > | $123.90
| > |
| > | This is how I would like the data to appear in the gridview:
| > |
| > | 456.95
| > | 200.89
| > | 34.95
| > | 123.90
| > |
| > | Hope this helps.
| > |
| > |
| > |
| >
|
|
|

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.