Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1377
  • Last Modified:

Show the currency formats

Hi Experts,
I want to display all the numbers in my web page,whether it being binded in Gridview or shown as a total of Gridview column in Indian currency format.Further I don't want indian rupee syntax there.

When I set the culture as
Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");

1389000 displayrs as 'Ru' 13,89,000.
But only where I am computing the number and displaying it as total.
I don't want to show Indian Style 'Ru' there and want only that format.
And I want to apply this format to gridview columns also.How to do this?
0
johny_bravo1
Asked:
johny_bravo1
  • 9
  • 9
  • 2
  • +1
3 Solutions
 
Rahul Goel ITILSenior Consultant - DeloitteCommented:
Hey while converting the number to INR format, you need to replace the string with "Ru" with ""
0
 
johny_bravo1Author Commented:

Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
 e.Row.Cells[5].Text = total_compensation.ToString("c");

These are the lines where I am getting the results.
How can I replace the "Ru"
0
 
DreamsTechCommented:
Simple Solution ,Use Ajax MaskedEditExtender and MaskedEditValidator.

In MaskedEditExtender ,set CurrencyFormat in Mask = "999,999,999,999.99" for INR ,similary u can set this value for diplaying currency format of any language.

Also,tie this extender to the Control diplaying the currency (Could be a ItemTemplate in a GridView)

eg :TargetControlID="txtPremium"

More details @ :http://www.asp.net/ajax/ajaxcontroltoolkit/samples/maskededit/maskededit.aspx



<cc1:MaskedEditExtender ID="meeLOBEstimatedPremium" runat="server" TargetControlID="txtPremium"
                                                                        Mask="999,999,999,999.99" OnFocusCssClass="MaskedEditFocus"
                                                                        OnInvalidCssClass="MaskedEditError" MaskType="Number" InputDirection="RightToLeft"
                                                                        AcceptNegative="None" DisplayMoney="None" />

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
johny_bravo1Author Commented:
I am using .Net 2.0
0
 
jinalCommented:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Label1.Text = int.Parse("1389000").ToString("N2", info);
OR

CultureInfo info = new CultureInfo("hi-IN");
Label1.Text = int.Parse("1389000").ToString("N2",info);
0
 
jinalCommented:
Sorry try this i made mistake in previous one.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Label1.Text = int.Parse("1389000").ToString("N2");
or

CultureInfo info = new CultureInfo("hi-IN");
Label1.Text = int.Parse("1389000").ToString("N2",info);
 
0
 
johny_bravo1Author Commented:
Thanks jinal,

Earlier my line was,
e.Row.Cells[5].Text = total_compensation.ToString("c");

e.Row.Cells[5].Text = decimal.Parse("1389000").ToString("N2");

I want to pass  total_compensation,which is calculated runtimw.Will u pls guide me?
0
 
jinalCommented:
use this way
I hope total_compensation is int or double.
e.Row.Cells[5].Text = total_compensation.ToString("N2");

0
 
johny_bravo1Author Commented:
Thanks works perfectly.
Can I set the format to the Boundfield of the Gridview.How?
0
 
Rahul Goel ITILSenior Consultant - DeloitteCommented:
total_compensation.ToString("c").Substring(2).Trim() here is your answer
0
 
johny_bravo1Author Commented:
Thanks Rahu_ketu_patal,that is solved.
I want to format the Boundfield in Gridview.
0
 
jinalCommented:
For GridView : <<yourfield>>  = yourdatasource field name

<asp:BoundField DataField="<<yourfield>>" DataFormatString="{0:N0}" />
0
 
johny_bravo1Author Commented:
:( Not working this time.
0
 
jinalCommented:
Please put your aspx code snippet and datasource code snippet over here.
So i can suggest you more detail solution for this.
0
 
johny_bravo1Author Commented:
<asp:GridView ID="gvKhateAcquisition" AutoGenerateColumns="false" runat="server" CellPadding="4" ForeColor="#333333" OnRowDataBound="gvKhateAcquisition_RowDataBound" ShowFooter="True" Width="99%" OnSelectedIndexChanged="gvKhateAcquisition_SelectedIndexChanged" OnRowCreated="gvKhateAcquisition_RowCreated">
                                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <RowStyle BackColor="#EFF3FB" />
                                    <EditRowStyle BackColor="#2461BF" />
                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <AlternatingRowStyle BackColor="White" />
                                    <Columns>
                                     <asp:BoundField DataField="Village_Name" HeaderText="Village" />
                                            <asp:BoundField DataField="khatedar_no" HeaderText="Khate No" />
                                            <asp:BoundField DataField="Khatedars" HeaderText="Khatedars" />
                                            <asp:BoundField DataField="new_survey_no" HeaderText="Survey No" />
                                            <asp:BoundField DataField="area_acquired" HeaderText="Area Acquired" />
                                            <asp:BoundField DataField="net_amount" HeaderText="Compensation" DataFormatString="{0:N0}"/>
                                            <asp:BoundField DataField="person" />
                                            <asp:BoundField DataField="Village_type" />
                                            </Columns>
                                </asp:GridView>

I am binding the Gridview in code behind.
0
 
jinalCommented:
Hello ,
I think you are binding DataTable . Can you please check what is the datatype of net_amount column in Datatable.
dt.Columns["net_Amout"].DataType
Are you using RowDataBound to update net_amout column value.
 
 
0
 
johny_bravo1Author Commented:
Hi,

I am binding  Dataset to the Gridview.

In RowDataBound I am calculating the running total to show in the footer,that's it.

 protected void gvKhateAcquisition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
           
        if (e.Row.RowType == DataControlRowType.DataRow)
            {

                // add the treeTotal and treeValueTotal to the running total variables
                total_area_acq += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "area_acquired"));
                total_compensation += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "net_amount"));

            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Total:";
                // for the Footer, display the running totals
                e.Row.Cells[4].Text = total_area_acq.ToString();
               
                e.Row.Cells[5].Text = total_compensation.ToString("N2");
                e.Row.Font.Bold = true;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvKhateAcquisition, "Select$" + e.Row.RowIndex);
            }
    }
0
 
jinalCommented:
Try this sample.
Let me know format is working or not.

/* Code Behind */
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    double total_area_acq = 0.0;
    decimal total_compensation = 0;
    CultureInfo info = new CultureInfo("hi-IN");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Village_Name", typeof(string));
            dt.Columns.Add("khatedar_no", typeof(string));
            dt.Columns.Add("Khatedars", typeof(string));
            dt.Columns.Add("new_survey_no", typeof(string));
            dt.Columns.Add("area_acquired", typeof(string));
            dt.Columns.Add("net_amount", typeof(string));
            dt.Columns.Add("person", typeof(string));
            dt.Columns.Add("Village_Type", typeof(string));
 
            DataRow dr = dt.NewRow();
            dr[0] = "1";
            dr[1] = "1";
            dr[2] = "1";
            dr[3] = "1";
            dr[4] = "100";
            dr[5] = "1000.00";
            dr[6] = "1";
            dr[7] = "1";
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr[0] = "2";
            dr[1] = "2";
            dr[2] = "2";
            dr[3] = "2";
            dr[4] = "200";
            dr[5] = "2000.00";
            dr[6] = "2";
            dr[7] = "2";
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr[0] = "3";
            dr[1] = "3";
            dr[2] = "3";
            dr[3] = "3";
            dr[4] = "300";
            dr[5] = "300000.00";
            dr[6] = "3";
            dr[7] = "3";
            dt.Rows.Add(dr);
 
            gvKhateAcquisition.DataSource = dt;
            gvKhateAcquisition.DataBind();
        }       
    }
    protected void gvKhateAcquisition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
            // add the treeTotal and treeValueTotal to the running total variables
            total_area_acq += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "area_acquired"));
            total_compensation += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "net_amount"));
 
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total:";
            // for the Footer, display the running totals
            e.Row.Cells[4].Text = total_area_acq.ToString();
 
            e.Row.Cells[5].Text = total_compensation.ToString("N2",info);
            e.Row.Font.Bold = true;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvKhateAcquisition, "Select$" + e.Row.RowIndex);
        }
 
    }
}
 
/* ASPX File */
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:GridView ID="gvKhateAcquisition" AutoGenerateColumns="false" runat="server" CellPadding="4" ForeColor="#333333" OnRowDataBound="gvKhateAcquisition_RowDataBound" ShowFooter="True" Width="99%"  >
                                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <RowStyle BackColor="#EFF3FB" />
                                    <EditRowStyle BackColor="#2461BF" />
                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <AlternatingRowStyle BackColor="White" />
                                    <Columns>
                                     <asp:BoundField DataField="Village_Name" HeaderText="Village" />
                                            <asp:BoundField DataField="khatedar_no" HeaderText="Khate No" />
                                            <asp:BoundField DataField="Khatedars" HeaderText="Khatedars" />
                                            <asp:BoundField DataField="new_survey_no" HeaderText="Survey No" />
                                            <asp:BoundField DataField="area_acquired" HeaderText="Area Acquired" />
                                            <asp:BoundField DataField="net_amount" HeaderText="Compensation" DataFormatString="{0:N0}"/>
                                            <asp:BoundField DataField="person" />
                                            <asp:BoundField DataField="Village_type" />
                                            </Columns>
                                </asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
jinalCommented:
One more thing...
When you display data in gridview it is not formatiing each row ( net_amout column) . It still display as string 300000.00 in my sample.  This is b'coz i consider DataType of column is string. If you want to format that to than column datatype either decima l or double, than and than DAtaFormatString works.
 
0
 
jinalCommented:
Use this if you want to format currency for each row in above sample.
protected void gvKhateAcquisition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal dtemp = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "net_amount"));
            // add the treeTotal and treeValueTotal to the running total variables
            e.Row.Cells[5].Text = dtemp.ToString("N2", info);
            total_area_acq += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "area_acquired"));
            total_compensation += dtemp;
 
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total:";
            // for the Footer, display the running totals
            e.Row.Cells[4].Text = total_area_acq.ToString();
 
            e.Row.Cells[5].Text = total_compensation.ToString("N2",info);
            e.Row.Font.Bold = true;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvKhateAcquisition, "Select$" + e.Row.RowIndex);
        }
 
    }

Open in new window

0
 
johny_bravo1Author Commented:
Works Great.Thanks a lot
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 9
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now