You must Sign In to post a response.
  • Category: ASP.NET

    Currency format in gridview

    Hi all,

    I am binding gridview at runtime (autogenerated columns true) without using boundfield or template in aspx file. Stored procedure is getting data into datatable and further gridview datasource is being bind with datatable.

    I want to apply currency to one of the column in datatable( but I dont want to do this in Stored procedure).

    Can you please tell me how can I apply currency to the dynamically created columns of gridview?

    Thanks in advance
  • #769076
    Hi,

    I guess you can a value in the web.config and use the values when ever is needed.


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


    and use the symbol from the config


    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 other details
    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();

    }


    Thanks,
    Mani

  • #769092
    Hi,

    Why you don't declare boundfield / templatefield?

    Is there any reason behind, if there is no reason then don't try to build the application more complex, my suggestion is use simple boundfield like below and apply DataFormatString what format you want and set HtmlEncode to false.



    <asp:BoundField DataField="Cost" DataFormatString="{0:c}" HtmlEncode="False" />


    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #769106
    Though it is dynamically created gridview but still it fires 'rowdatabound' event when data gets attached to its 'data source' property.
    Try to use that event and check the column name and replace it with the currency sign
    below is the code snippet

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    TableCell statusCell = e.Row.Cells[2];
    if (statusCell.Text == "A")
    {
    statusCell.Text = "Absent";
    }
    if (statusCell.Text == "P")
    {
    statusCell.Text = "Present";
    }
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #769132
    This is the example of how to convert string in to currency format such as @price.ToString("C", culture)
    public static class Cultures
    {
    public static readonly CultureInfo UnitedKingdom =
    CultureInfo.GetCultureInfo("en-GB");
    }

    Then:

    @price.ToString("C", Cultures.UnitedKingdom)


  • Sign In to post your comments