472,973 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

Gridview Item formatting

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
3 6163
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: euan | last post by:
HI Guys, I have bee using conditional formatting in the datagrid recently and I am moving over to framework 2.0 and noticed the datagrid has been replaced by the gridview. So, I would like to do...
4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
2
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
2
by: RCM | last post by:
I have a very simple gridview that is working fine. My real data is more complicated, but for purposes of my question imagine a grid display that looks like: Audi red heavy Audi blue medium...
0
by: rmgalante | last post by:
Hi, I've been experimenting with the ASP.Net GridView and encountered some interesting issues that I thought I would share. I have a page that loads a GridView with a generic collection of...
0
by: Adam Sandler | last post by:
Hello, My codebehind builds a GridView at runtime and shovels the data off as a part of a callback... Dim returnstring As String = String.Empty Dim gv As New GridView gv.DataSource = ds...
1
by: =?Utf-8?B?V2VzbGV5IERhdmlzLCBHZW5lcmFsIER5bmFtaWNz | last post by:
I'm moving from years with the datagrid to a new project, .net 2.0, using GridView controls. Per past practice, it is often a lot easier to inject controls (or special formatting) in RowDataBound...
5
by: Mark B | last post by:
I'd like to have a field in a gridview (or standalone on a webpage) that not only drops down each salespersons name but also precedes their name with a blue, red, green or orange dot icon depicting...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.