Monday 23 February 2015

How to apply multiple Cultures on salary column in Asp.net GridView



How  to apply multiple Cultures on salary column in Asp.net GridView


Note: Here we have discuss multiple cultures for the single page for example each row of GridView display a record for the person belonging to different Country

Use the code belowto get the list of all supported cultures in Asp.net

// using this  namespace

using System.Globalization;


protected void Button1_Click(object sender, EventArgs e)
{
string ss = "<table><tr ><th style='width:150px'>Country Name</th><th style='width:150px'>CulturName</th>";

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
ss += "<tr><td>" + ci.Name + "</td><td>" + ci.DisplayName + "<td><tr>";

}
ss += "</table>";
Literal1.Text = ss;

}

Result




RowDataBound Event : It is raised when a record in the datasource is bound to a row in GridView Control

use test

create table Employee
(
EmpId int  primary key,
Name varchar(50),
Salary int,
CountryName varchar(50)
)
Source Code for Default.aspx…………………………..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmpId" DataSourceID="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True"
SortExpression="EmpId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Salary" HeaderText="Salary" DataFormatString="{0:c}"
SortExpression="Salary" />
<asp:BoundField DataField="CountryName" HeaderText="CountryName"
SortExpression="CountryName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [Employee] ORDER BY [EmpId]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

Source Code for  Default.aspx.cs…………………………..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

// code with RowDataBound Event........................

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text == "US")
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-Us");
}
else  if (e.Row.Cells[3].Text == "India")
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-IN");
}
else if (e.Row.Cells[3].Text == "South Africa")
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-ZA");
}
else if (e.Row.Cells[3].Text == "Malaysia")
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-MY");
}
else if (e.Row.Cells[3].Text == "UK")
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
}

}
}
}

Result: Here this row is also process with the current default Culture US ("en-Us")  
Because we are setting the thread culture for the next row basically






Note: for the above error, solution is given below

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

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

if (e.Row.Cells[3].Text == "US")
{

//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-Us");
int salary = Convert.ToInt32(e.Row.Cells[2].Text);
string formattedstr = string.Format(new System.Globalization.CultureInfo("en-Us"), "{0:c}", salary);
e.Row.Cells[2].Text = formattedstr;

}
else if (e.Row.Cells[3].Text == "India")
{
int salary = Convert.ToInt32(e.Row.Cells[2].Text);
string formattedstr = string.Format(new System.Globalization.CultureInfo("en-IN"), "{0:c}", salary);
e.Row.Cells[2].Text = formattedstr;

}

else if (e.Row.Cells[3].Text == "South Africa")
{

int salary = Convert.ToInt32(e.Row.Cells[2].Text);
string formattedstr = string.Format(new System.Globalization.CultureInfo("en-ZA"), "{0:c}", salary);
e.Row.Cells[2].Text = formattedstr;

}

else if (e.Row.Cells[3].Text == "Malaysia")
{

int salary = Convert.ToInt32(e.Row.Cells[2].Text);
string formattedstr = string.Format(new System.Globalization.CultureInfo("en-MY"), "{0:c}", salary);
e.Row.Cells[2].Text = formattedstr;

}
else if (e.Row.Cells[3].Text == "UK")
{

int salary = Convert.ToInt32(e.Row.Cells[2].Text);
string formattedstr = string.Format(new System.Globalization.CultureInfo("en-GB"), "{0:c}", salary);
e.Row.Cells[2].Text = formattedstr;

}

}


Then the following error is coming up

Server Error in '/GridViewConcept' Application

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.


Source Error:

Line 53:             else if (e.Row.Cells[3].Text == "India")
Line 54:             {
Line 55:                 int salary = Convert.ToInt32(e.Row.Cells[2].Text);
Line 56:                 string formattedstr = string.Format(new System.Globalization.CultureInfo("en-IN"), "{0:c}", salary);
Line 57:                 e.Row.Cells[2].Text = formattedstr;


For removing the above error we will go to the GridView source of the default page and remove the DataFormatString="{0:c}" from grid view bound field because it is a render with salary symbol hence it cannot be converted into integer property

For Example:
 
<asp:BoundField DataField="Salary" HeaderText="Salary"   DataFormatString="{0:c}"
SortExpression="Salary" />


Result:




0 comments:

Post a Comment