简体   繁体   English

将ItemTemplate GridView货币项目格式化为千位分隔符ASP.NET?

[英]Format ItemTemplate GridView money item to thousand seperator ASP.NET?

I have the Salary field in SQL Server table with money type, in the GridView ItemTemplate I use: 在我使用的GridView ItemTemplate中,SQL Server表中的Salary字段具有货币类型:

<asp:TemplateColumn ItemStyle-CssClass="Text">
    <HeaderTemplate>Basic Salary</HeaderTemplate>
        <ItemTemplate>
            <%# DataBinder.Eval(Container, "DataItem.BasicSalary", "{0:0,0}")%>
        </ItemTemplate>
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateColumn>

but it only display:6500000.0000 intead of my desired one is : 6,500,000. 但它只显示:6500000.0000 intead我想要的是:6,500,000。 So how to do ? 那该怎么办呢? Please help! 请帮忙! If I use this fuction: 如果我使用此功能:

protected string Format_Number(string Number)
{
    Number = Number.Replace(".", "");
    Number = Number.Replace(",", "");
    var tmp = "";
    while (Number.Length > 3)
    {
        tmp = "." + Number.Substring(Number.Length - 3) + tmp;
        Number = Number.Substring(0, Number.Length - 3);
    }
    tmp = Number + tmp;
    return tmp;
}

then in the ItemTemplate: 然后在ItemTemplate中:

<ItemTemplate>
    <asp:label ID="lblSalary" 
               runat="server" 
               Text='<%# Format_Number(DataBinder.Eval(Container.DataItem, "Salary").ToString()) %>'>
    </asp:label>

the output is: 6.500.000.000 I want to cutout the last 3 digit. 输出为:6.500.000.000我想切除最后3位数字。 How to do? 怎么做?

Anyway.. you convert it to decimal then reformat it should work 无论如何..您将其转换为十进制然后重新格式化应该可以工作

 protected string Format_Number(string Number)
 {
    // Variable
    string value = string.Empty;
    decimal castValue = 0;
    bool isValid = false;

    // Check
    if (Number != string.Empty)
    {
        // Parse
        isValid = decimal.TryParse(Number, out castValue);

        // Check & Set Decimal Valud
        if (isValid) value = castValue.ToString("0,0");
    }


    return value;
 }

Since it's a string, why not simply use a substring? 既然是字符串,为什么不简单地使用子字符串呢?

just like: 就像:

return tmp.Substring(0, tmp.Length - 4);

May be you can try this 也许你可以试试这个

salary = salary.Replace(".", "");
salary = salary.Replace(",", "");
string tmp = (Convert.ToInt64(salary)).ToString("N", new CultureInfo("en-US"));

You don't use any function "Format_Number(string Number)". 您不使用任何函数“ Format_Number(string Number)”。 Directly bind as money data type. 直接绑定为货币数据类型。 This is work for me. 这对我来说是工作。

<asp:TemplateField HeaderText="Basic Salary">
                <HeaderTemplate>Basic Salary</HeaderTemplate>
                <ItemTemplate>
                    <%# DataBinder.Eval(Container, "DataItem.BasicSalary", {0:0}")%>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
            </asp:TemplateField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM