将十进制数格式化为印度货币格式

时间:2011-11-14 04:41:53

标签: c# asp.net gridview formatting

我在我的项目中使用C#asp.net 4.0,我需要以印度电流格式显示价格。

  

例如。我的号码是12550000.00然后我想把它显示为1,25,50,000.00

但我希望在将数据绑定到gridview时将其显示在gridview中,

所以可以在标记页面中完成。我们为每个项目数据绑定放置Eval。

但是,我还想解释我的senario显示逗号分隔值。

我在gridview上方有一组文本框,用户可以在其中输入值,然后单击“添加”。

这会在视图状态中添加,并且视图状态绑定到gridview。

在gridview中,我点击它时也有Edit按钮,viewstate中的值被传递回gridview的RowCommand Event上的文本框。并在更新时单击viewstate数据表已更新并已绑定回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的标记,

 <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" />
                                &nbsp;
                                <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>

1 个答案:

答案 0 :(得分:3)

使用ToString函数中的“C”参数,并确保设置globalaztion属性。

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元素,而不是将其从当前线程中删除。

OP编辑的额外信息

好吧,你想要把它放到你的网格中,是想创建一个PROTECTED函数,它接收要转换的数字,并返回转换后的字符串(这基本上就是上面的代码。

现在,在ASPX方面,您需要在网格视图中使用该功能 所以,而不是:

 <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模板中实例化额外的LABEL。

我没有做额外标签的原因是因为我们在那里不需要它。这只是你不需要承担的更多处理器/内存开销。

至于将未格式化的值传递给文本字段,这是因为它最终会更容易验证,而不必解析逗号和其他字符串元素。