问题描述
我在我的项目中使用 C# asp.net 4.0,我需要以印度货币格式显示价格.
I am using C# asp.net 4.0 in my project where i need to display price in India curreny format.
例如.我的号码是 12550000.00 然后我想将其显示为 1,25,50,000.00
eg. my number is 12550000.00 then i want to display it as 1,25,50,000.00
但是当我将数据绑定到gridview时,我希望它显示在gridview中,
But i want this to be displayed in gridview when i bind the data to the gridview,
所以它可以在标记页面中完成.我们为每个 Item Data Bound 放置 Eval.
so it can be done in markup page. where we place Eval for each Item Data Bound.
但是,我还想解释一下我的 senario 以显示逗号分隔值.
However, i would also like to explain my senario for displaying comma sepearted values.
我在网格视图上方有一组文本框,用户在其中输入值并单击添加.
i have a set of textboxes above the gridview where user makes entries of values and click add.
这会添加到视图状态中,并且视图状态绑定到 gridview.
this gets add in the viewstate and the viewstate is binded to gridview.
在gridview中,我也有点击它的编辑按钮,viewstate中的值被传递回gridview的RowCommand事件上的文本框.并在更新时单击 viewstate 数据表更新并绑定回 gridview.
In gridview i also have Edit button on click of it the values in viewstate is passed back to textbox on RowCommand Event of gridview. and on update click the viewstate datatable is updated and Binded back to gridview.
供您参考:
protected void gvPropertyConfig_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "EditItem")
{
int index = Convert.ToInt32(e.CommandArgument);
hdnIndex.Value = index.ToString();
DataTable dt = (DataTable)ViewState["proeprtyConfig"];
DataRow dr = dt.Rows[index];
if (Request.QueryString["CMD"] == "Edit")
{
hdnPropertyConfigID.Value = dr["config_id"].ToString();
if (dr["is_active"].ToString().ToLower() == "true")
{
chkConfigVisible.Checked = true;
}
else
{
chkConfigVisible.Checked = false;
}
thIsActHed.Visible = true;
tdIsActchk.Visible = true;
tdbtnConfig.ColSpan = 2;
}
txtScalableArea.Text = dr["scalable_area"].ToString();
txtCarpetArea.Text = dr["carpet_area"].ToString();
txtPricePerSqFt.Text = dr["price_per_sq_ft"].ToString();
txtCCPricePerSqFt.Text = dr["cc_price_per_sq_ft"].ToString();
txtTotalPrice.Text = dr["total_price"].ToString();
ddlNoOfBedrooms.SelectedValue = dr["room_id"].ToString();
btnUpdateConfig.Visible = true;
btnConfigSubmit.Visible = false;
}
if (e.CommandName == "DeleteItem")
{
int index = Convert.ToInt32(e.CommandArgument);
DataTable dt = (DataTable)ViewState["proeprtyConfig"];
DataRow dr = dt.Rows[index];
if (Request.QueryString["CMD"].ToString() == "Edit")
{
int PropertyConfigID = Convert.ToInt32(dr[0].ToString());
prConfigObj.deletePropertyConfig(PropertyConfigID);
fillData();
}
else
{
dr.Delete();
gvPropertyConfig.DataSource = (DataTable)ViewState["proeprtyConfig"];
gvPropertyConfig.DataBind();
}
clearConfigTextBoxes();
btnConfigSubmit.Visible = true;
btnUpdateConfig.Visible = false;
}
setChecklistAttr();
}
catch (Exception ex)
{
throw ex;
}
}
下面是 Gridview 的标记,
Below is the markup for Gridview,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="tabBord">
<table>
<tr>
<td colspan="4" class="middle">
<h4>
Property Config Information</h4>
</td>
</tr>
<tr>
<td colspan="4">
<p>
Note: Enter total prices in lacs only. Eg. If 1 Crore than enter 1,00,00,000
</p>
<p>
</p>
</td>
</tr>
<tr>
<td>
<div id="divconfigstr" runat="server">
Configuration<span style="color: Red">*</span></div>
<%--class="displaynon"--%>
</td>
<td>
<div id="divnoofbedrooms" runat="server">
<asp:DropDownList Enabled="false" ID="ddlNoOfBedrooms" runat="server">
</asp:DropDownList>
<p>
</p>
</div>
</td>
<td>
Scalable Area <span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtScalableArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td>
Carpet Area <span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtCarpetArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
<td>
Price/Sq.Ft.<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td>
CC Price/Sq.Ft.<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtCCPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
<td>
Total Price (in lacs)<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtTotalPrice" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td id="thIsActHed" runat="server">
Active
<asp:HiddenField ID="hdnPropertyConfigID" runat="server" />
<asp:HiddenField ID="hdnIndex" runat="server" />
</td>
<td id="tdIsActchk" runat="server">
<asp:CheckBox ID="chkConfigVisible" runat="server" CssClass="checklist" /><p>
</p>
</td>
<td id="tdbtnConfig" runat="server" colspan="2">
<div class="btnHold">
<asp:Button ID="btnConfigSubmit" runat="server" Text="Add" OnClientClick="return ValidatePropertyConfig();"
CssClass="sendBtn" OnClick="btnConfigSubmit_Click" />
<asp:Button ID="btnUpdateConfig" runat="server" OnClick="btnUpdateConfig_Click" OnClientClick="return ValidatePropertyConfig();"
CssClass="sendBtn" Text="Update" Visible="False" />
<asp:Label ID="lblerrconfig" CssClass="errormsg" runat="server"></asp:Label>
</div>
</td>
</tr>
<tr>
<td colspan="4">
<div class="pHold">
<div class="gridH">
<asp:GridView ID="gvPropertyConfig" runat="server" AutoGenerateColumns="False" OnRowCommand="gvPropertyConfig_RowCommand"
OnRowDataBound="gvPropertyConfig_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="No Of Bedrooms" ItemStyle-CssClass="txtLT">
<ItemTemplate>
<asp:Label ID="lblno_of_bedrooms" runat="server" Text='<%# Eval("room_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scalable Area" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Carpet Area" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price/SqFt." ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtprice_per_sq_ft" runat="server" Text='<%# Eval("price_per_sq_ft") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblprice_per_sq_ft" runat="server" Text='<%# Eval("price_per_sq_ft") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CC Price/SqFt." ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtcc_price_per_sq_ft" runat="server" Text='<%# Eval("cc_price_per_sq_ft") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcc_price_per_sq_ft" runat="server" Text='<%# Eval("cc_price_per_sq_ft") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price (in lacs)" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server" Text='<%# Eval("total_price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal_price" runat="server" Text='<%# Eval("total_price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-CssClass="txtLT">
<ItemTemplate>
<asp:ImageButton runat="server" ID="btnEditItem" CssClass="edBtn" ImageUrl="~/Admin/Includes/Images/edit.png"
ToolTip="Edit Item" CommandName="EditItem" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<asp:ImageButton runat="server" ID="btnDeletetem" CssClass="edBtn" ImageUrl="~/Admin/Includes/Images/delete.png"
CommandName="DeleteItem" ToolTip="Delete Item" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
推荐答案
在 ToString 函数中使用C"参数,并确保设置了 globalaztion 属性.
Use the "C" parameter in the ToString function, and make sure you set the globalaztion attributes.
string parseValueIntoCurrency(double number) {
// set currency format
string curCulture = Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new
System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;
return number.ToString("c", currencyFormat);
}
如果您想使用不同的文化(假设您在美国,并且想要印度格式),那么只需使用适当的 CultureInfo 元素,而不是将其从当前线程中取出.
If you want to use a different Culture (say you're in USA, and you want the Indian format) then just use the appropriate CultureInfo element rather than getting it out of the current thread.
因 OP 编辑而产生的额外信息
好吧,你想要做的是把它放到你的网格中,是创建一个 PROTECTED 函数,它接收要转换的数字,并返回转换后的字符串(这基本上是上面的代码.
All right, what you're wanting to do to get this into your grid, is to create a PROTECTED function which takes in the number to be converted, and returns the converted string (this is basically the code above.
现在,在 ASPX 端,您需要在网格视图中使用该功能.
所以,而不是这个:
Now, on the ASPX side, you need to use that function in your grid view.
So, instead of this:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal_price" runat="server"
Text='<%# Eval("total_price") %>'> />
</ItemTemplate>
</asp:TemplateField>
您将使用此模板字段:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<%# parseValueIntoCurrency(Eval("total_price")) %>'>
</ItemTemplate>
</asp:TemplateField>
注意,有两件事.首先是我仍在将 UNFORMATTED 值传递给 EDIT TEMPLATE,并且我没有在 ITEM TEMPLATE 中实例化额外的 LABEL.
Note, two things. The first is that I'm still passing the UNFORMATTED value into the EDIT TEMPLATE, and that I'm not instantiating an extra LABEL in the ITEM TEMPLATE.
我不做额外标签的原因是我们不需要它.这只是您不需要承担的更多处理器/内存开销.
The reason I'm not doing the extra label, is because we just don't need it in there. That's just a bit more processor/memory overhead that you just don't need to incur.
至于将未格式化的值传递给文本字段,这是因为它最终会更容易验证,而无需解析出逗号和其他字符串元素.
As for passing the unformatted value to the text field, that's because it'll ultimately be easier to validate without having to parse out the commas and other string elements.
这篇关于将十进制数格式化为印度货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!