RSS

How to Add currency Symbol in Gridview Price/Amount Column

21 Sep

A.A…………

If you want to add currency symbol in gridview column. dynamically change the symbol of currency just you need the symbol in web.config file and automatically change the symbol in gridview…….

Solution:

Gridview formate

————————————————————–

<asp:GridView ID=”GVInstallment” runat=”server” AutoGenerateColumns=”False” DataKeyNames=”InstallmentID”
OnRowDeleting=”GVInstallment_RowDeleting” Width=”100%” OnRowDataBound=”GVInstallment_RowDataBound”>
<Columns>
<asp:TemplateField HeaderText=”Amount”>
<ItemTemplate>
<asp:Label ID=”lblAmountgv” runat=”server” Text='<%# Bind(“Amount”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”DueDate” DataFormatString=”{0:d}” HeaderText=”Due Date” />
<asp:BoundField DataField=”Status” HeaderText=”Status” />
<asp:TemplateField HeaderText=”Action”>
<ItemTemplate>
<asp:Button ID=”btnDelte” runat=”server” CommandName=”Delete” SkinID=”Delete” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<center>
<asp:Label ID=”lblEmptyGV” runat=”server” Font-Bold=”True” ForeColor=”White” Text=”No Records Found of Installment”></asp:Label></center>
</EmptyDataTemplate>
</asp:GridView>

—————————————————————-

Currency Symbol use in Web.Config File

<add key=”Currency” value=”£”/>

Access in Code behind File where we use this symbol

Declare the variable.

public static string CurrenceSymble = string.Empty;

Write this code on page load and use this namespace

using System.Configuration;

CurrenceSymble = ConfigurationManager.AppSettings.Get(“Currency”);
lblRegFeeCrSybble.Visible = true;
lblRegFeeCrSybble.Text = CurrenceSymble;

Write this code Gridview Event on RowDataBound

protected void GVInstallment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// add the UnitPrice and QuantityTotal to the running total variables
priceTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, “Amount”));
((Label)e.Row.FindControl(“lblAmountgv”)).Text =lblRegFeeCrSybble.Text + ‘ ‘ + priceTotal;

}

//if(e.Row.RowType == DataControlRowType.DataRow)

//       ((Label)e.Row.FindControl(“lblAmountgv”)).Text  =((DataRowView)e.Row.DataItem)[“Amount”].ToString();

}

—————————————————————–

———————Complete—————————————————————

Second  Tutorial which also very help full

How to format GridView Rows/Columns in Design/Run time

First of All if you use static or hard code use when you need only one formate then use default method.

Here i will discuss how you can format data in GridView rows. Most of the time we require to render our GridView control by custom formatting since data source format does not contain our exact required formats. Like, you want to display numeric values in two decimal format or want to display short date instead of considering time portion from databse. In this post i will try to resolve below issues:

*** How to format date/numeric data in GridView in design time
*** How to format data in GridView in run time/code behind
*** How to format Template Column
*** How to use DataFormatString property to format data
*** What is HtmlEncode?

Format character Description Pattern Example
d Short date MM/dd/yyyy 12/1/2009
D Long date dddd, dd MMMM yyyy Sunday, April 12, 2009
t Short time HH:mm 5:12 PM
T Long time HH:mm:ss 5:12:00 PM
f Full date/time(short time) dddd, dd MMMM yyyy HH:mm Sunday, April 12, 2009 5:12 PM
F Full date/time(long time) dddd, dd MMMM yyyy HH:mm:ss Sunday, April 12, 2009 5:12:00 PM
g General date/time(short time) MM/dd/yyyy HH:mm 4/12/2009 5:12 PM
G General date/time(long time) MM/dd/yyyy HH:mm:ss 4/12/2009 5:12:00 PM
m or M Month day MMMM dd April 12
r or R GMT ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Sun, 12 Apr 2009 7:12:00 GMT
s Sortable date/time yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2009-04-12T14:12:00
u UniversalSortableDateTimePattern using universal time yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2009-04-12 14:12:00z
U Full date and time (long date and long time) using universal time dddd, dd MMMM yyyy HH:mm:ss Sunday, April 12, 2009 10:12:00 PM
y or Y Year month MMMM yyyy April, 2009
Format date in bound column:

Consider we have a GridView that displays Supplier information with Code, Name, Address, Contact no and Supply date where the Supply date is stored with time in database. To do that for bound column apply the below technique:

<

asp:BoundField DataField=”LastDelivery” HeaderText=”Recent Delivery” DataFormatString = “{0:y}” HtmlEncode=”false”></asp:BoundField>

OR

<

asp:BoundField DataField=”LastDelivery” HeaderText=”Recent Delivery” DataFormatString = “{0:MMMM yyyy}” HtmlEncode=”false”></asp:BoundField>

If you want to format date using SQL query then Click Here.

i.e. You can use both format or pattern column to format date column like below:

Formatting columnar data in a GridView code-behind:

protected void gvEdit_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType == DataControlRowType.DataRow)
{e.Row.Cells[4].Text = Convert.ToDateTime(((DataRowView)e.Row.DataItem)[“LastDelivery”]).ToString(“y”);
}
}

Format Template Column:
Add a template column with textbox/label etc.. then format it in the below way:

<

asp:TemplateField HeaderText=”Recent Delivery”>
<ItemTemplate><asp:Label ID=”lbl” runat=”server” Text='<%# Eval(“LastDelivery”, “{0:y}”) %>’></asp:Label>
</ItemTemplate></asp:TemplateField>

List of Numeric Formats:

Format Description Example
C Currency format $10.00
D Decimal format 10
E Scientific format 1.000000E+001
F Fixed format 10.00
G General format 10
N Number format 10.00
X Hexadecimal format A

Note: Format characters are not case-sensitive, except for “X”

The value after the format character specifies the number of significant digits or decimal places to display. For example, the formatting string “{0:F2}” displays a fixed-point number with two decimal places.

Format numeric data in bound column:

<

asp:BoundField DataField=”Price” HeaderText=”Price” DataFormatString=”{0:C2}” HtmlEncode=”false”>

Format numeric data in Template Column:

<

asp:TemplateField HeaderText=”Recent Delivery”>
<ItemTemplate>
<asp:Label ID=”lbl” runat=”server” Text='<%# Eval(“Price”, “{0:C2}”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Formatting columnar data in a GridView code-behind:

protected void gvEdit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
((Label)e.Row.FindControl(“lbl”)).Text = Convert.ToDecimal(((DataRowView)e.Row.DataItem)[“ID”]).ToString(“C5”);
}

You may need to add few prefix like AU/US then use :
DataFormatString=”AU {0:C5}” HtmlEncode=”false”
OR
DataFormatString=”US {0:C5}” HtmlEncode=”false”

NOTE: Most of the beginner forget to add HtmlEncode=”false”

What is HtmlEncode?
Microsoft introduced this new property to prevent cross site scripting (CSS) attacks. This way if there is any malicious text in the fields, it will get encoded and will not executeon client’s browser. If this property is set to true, the formatting implementation in the control first encoded the text using HttpUtility.HtmlEncode. And after that it calls String.Format method on the encoded text value.Where as when HtmlCode is set to false, the formatting is done on the data value itself. So the way to fix your problem with formatting would be to set HtmlEncode property to false.


Advertisements
 
7 Comments

Posted by on September 21, 2010 in ASP Dot Net C#

 

7 responses to “How to Add currency Symbol in Gridview Price/Amount Column

  1. Niranjan Beherea

    January 7, 2011 at 5:46 am

    Thank you, for your all Currency format.
    Best of luck your future.

     
    • humrahimcs

      January 9, 2011 at 11:40 am

      thank for comments
      Same to you.

       
  2. uday

    November 18, 2011 at 11:14 am

    how can i store contact no with prefix 0 (zero) when export into excel sheet from web page

     
  3. K.SrinivasaRao

    November 10, 2012 at 4:14 am

    thanks brother it helps me a lot

     
  4. K.SrinivasaRao

    November 10, 2012 at 4:56 am

    my results comes like this USRs. 6,000.00000… but i need not to come Rs …and insted of that i want $ symbol and also i want indian curency symbol….could you please help me…….

     
  5. Ryanlame

    October 18, 2013 at 9:55 am

    Use the GridView to conditionally format rows in grid column.

     
  6. Vikas Sharma

    December 20, 2015 at 8:47 pm

    It’s Very Simple See This Video

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: