Scenario
Consider a table named Employees having the columns Id, Name, Designation and Salary and you were asked to display all the data on a web page. Also the salary should be displayed with the India Currency Symbol prefixed to it. The layout of the table is as follows:
The requirement is quite simple and can be done using a GridView. The data can simply be displayed over a web page using a Grid View. The following code shows how to display the data using a Web Form (EmployeeDetails.aspx) and a Grid View.
The following is the EmployeeDetails.aspx page:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeDetails.aspx.cs" Inherits="GridView.EmployeeDetails" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Employee Details</title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>Employee Details</h3>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" />
- <asp:BoundField DataField="Name" HeaderText="Name" />
- <asp:BoundField DataField="Designation" HeaderText="Designation" />
- <asp:BoundField DataField="Salary" HeaderText="Salary" />
- </Columns>
-
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
The following is the EmployeeDetails.cs page:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace GridView
- {
- public partial class EmployeeDetails : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string connect = ConfigurationManager.ConnectionStrings["SQLConnect"].ConnectionString;
- string query = "SELECT * from Employees;";
-
- using(SqlConnection con = new SqlConnection(connect))
- {
- SqlCommand cmd = new SqlCommand(query, con);
- con.Open();
- SqlDataReader dr = cmd.ExecuteReader();
-
- GridView1.DataSource = dr;
- GridView1.DataBind();
- }
- }
- }
- }
It should be noted that the ADO.Net code is not optimized here and is used only for this demo. In real projects, Stored Procedures and other conventions should be used for the query. Now run the application and the output is as follows:
The preceding image shows all the data from the database to a grid view control but our requirement is to display the currency symbol with the Salary to make the data more readable. To do this, we can benefit from a quick fix by adding property the DataFormatString to the Bound Field of the grid view as follows:
EmployeeData.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" EmployeeDetails.aspx.cs" Inherits="GridView. EmployeeDetails " %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Employee Details</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>Employee Details</h3>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" />
- <asp:BoundField DataField="Name" HeaderText="Name" />
- <asp:BoundField DataField="Designation" HeaderText="Designation" />
- <asp:BoundField DataField="Salary" HeaderText="Salary" DataFormatString="{0:c}" />
- </Columns>
-
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
If we run the application now, it displays the output as:
You have noticed that the $ sign is prefixed with the Salary values. Our requirement is to display the Rupee sign with the Salary but it shows the $ symbol. Can you guess why that is?
This is because we asked the Grid View to format the Salary column and append the default currency symbol from the culture language system with it. The default language of my OS is "en-US", in other words United States-English and the currency symbol for USA is $ (dollar). This is the reason it appends the $ sign with Salary values.
As in our requirement, we need to tweak the language settings from United States to India and to specify the currency symbol for Indian Rupee. The HTML encoded value of Indian rupee is "₹” To do this, go to the EmployeeDetails.cs page and override the InitializeCulture method as follows:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace GridView
- {
- public partial class EmployeeDetails : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string connect = ConfigurationManager.ConnectionStrings["SQLConnect"].ConnectionString;
- string query = "SELECT * from Employees;";
-
- using(SqlConnection con = new SqlConnection(connect))
- {
- SqlCommand cmd = new SqlCommand(query, con);
- con.Open();
- SqlDataReader dr = cmd.ExecuteReader();
-
- GridView1.DataSource = dr;
- GridView1.DataBind();
- }
- }
-
- protected override void InitializeCulture()
- {
- CultureInfo ci = new CultureInfo("en-IN");
- ci.NumberFormat.CurrencySymbol = "₹";
- Thread.CurrentThread.CurrentCulture = ci;
-
- base.InitializeCulture();
- }
- }
- }
-
Also add the HtmlEncode="false" to the bound filed of the GridView:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" EmployeeDetails.aspx.cs" Inherits="GridView. EmployeeDetails" %>
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Employee Details</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>Employee Details</h3>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" />
- <asp:BoundField DataField="Name" HeaderText="Name" />
- <asp:BoundField DataField="Designation" HeaderText="Designation" />
- <asp:BoundField DataField="Salary" HeaderText="Salary" DataFormatString="{0:c}" HtmlEncode="false" />
- </Columns>
-
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
-
If you run the application now, you'll see the Rupee symbol along with the Salary of each employee.
Conclusion
Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the Universe!