Hello,
Please is it possible to add a currency symbol to a GridView BoundField?
I want to add a currency symbol close to the figures as shown in the image below. The details is that, I have a dropdown list where a user will select the currency symbol of choice and the symbol will be save in a database field then when the user navigates to another page the currency symbol will be displayed alongside the amount. In the below screenshot, I only have plain figure without currency symbol in front of them.
At first, I had this idea to include the symbol along in a JavaScript code so that when a user selects the currency from dropdown list and is inputting data into the respective textboxes, the currency will show beside the amount when the amount is also displayed. But I found it difficult to do it in the JavaScript code, so I decided to use label to save the currency into the database field instead, and then display it along with the amount. Please any ideas and help?
This is my GridView when data is inputted into the GridView.
<asp:GridView ID="Gridview1" runat="server" class="table" Font-Size="14px" HeaderStyle-Font-Size="15px" AutoGenerateColumns="False" Style="max-width: 100%" RowStyle-Height="40px" HeaderStyle-ForeColor="#00003D"
BorderStyle="None" GridLines="None" Height="50px" HeaderStyle-Height="28px" HeaderStyle-BackColor="#fdfdfd">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Description" ItemStyle-Width="70%" HeaderStyle-Font-Bold="false" />
<asp:BoundField DataField="Qty" HeaderText="Quantity" ItemStyle-Width="15%" HeaderStyle-Font-Bold="false" />
<asp:BoundField DataField="Rate" HeaderText="Rate" ItemStyle-Width="19%" HeaderStyle-Font-Bold="false" />
<asp:BoundField DataField="Amount" HeaderText="Amount" ItemStyle-Width="20%" HeaderStyle-Font-Bold="false" />
</Columns>
<HeaderStyle Height="25px" />
</asp:GridView>
Here is how my database looks.
|
Qty
|
Price
|
Amount
|
CSymbol
|
sweets,bread
|
2,3
|
5,20
|
10,60
|
$
|
cake,noodle
|
3,5
|
10,40
|
30,200
|
£
|
*CSymbol = currency symbol
That's how my database table looks above. You can see that the CSymbol column has other currencies as well. So if I use this will it produce the desired outcome?
Then this is my code that fetches the data from the database.
The first code fetches item, Quantity, Price and Amount. Now the issue is to also fetch the currency symbol and display beside the amount and price.
Then, this code below fetches other related data.
private void DisplayValueData()
{
try
{
DataTable dt = new DataTable();
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Item,Qty,Price,Amount FROM DocTable WHERE receipt_no = @receipt_no", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@receipt_no", lblprefix.Text.Trim());
con.Open();
cmd.CommandType = CommandType.Text;
sda.SelectCommand = cmd;
sda.Fill(dt);
DataTable dt1 = dt.Clone();
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dr["Item"].ToString().Split(',').Length; i++)
{
if (!string.IsNullOrEmpty(dr["Item"].ToString().Split(',')[i]))
{
DataRow dr1 = dt1.NewRow();
dr1["Item"] = dr["Item"].ToString().Split(',')[i];
dr1["Qty"] = dr["Qty"].ToString().Split(',')[i];
dr1["Price"] = dr["Price"].ToString().Split(',')[i];
dr1["Amount"] = dr["Amount"].ToString().Split(',')[i];
dt1.Rows.Add(dr1.ItemArray);
}
}
Gridview1.DataSource = dt1;
Gridview1.DataBind();
}
con.Close();
}
}
}
DisplayData();
}
catch (SqlException ex)
{
string msg = "Error:"; msg += ex.Message; throw new Exception(msg);
}
}