繁体   English   中英

在ASP.NET C#中为动态gridview单元格设置货币

[英]Set currency for dynamic gridview cells in asp.net c#

我有一个动态的gridview,并且需要对某些列应用格式。

码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                string strHeader = GridView1.HeaderRow.Cells[i].Text;
                List<string> lstCurrency = new List<string>() { "Price", "Amount" };
                bool checkAmount = lstCurrency.Exists(o => strHeader.Equals(o));
                if (checkAmount)
                {
                    e.Row.Cells[i].Text = String.Format("{0:C2}", e.Row.Cells[i].Text);
                }
            }
        }
    } 

如果gridview列标题文本包含“ Price或“ Amount则需要应用货币格式,并且以上代码不起作用。

如果我做错了,请纠正我。

您需要将字符串e.Row.Cells[i].Text转换为decimal

e.Row.Cells[i].Text = String.Format("{0:C2}", Convert.ToDecimal(e.Row.Cells[i].Text));

您是否尝试过解析值? 根据需要使用Int或Decimal。

e.Row.Cells[i].Text = String.Format("{0:C2}", Int32.Parse(e.Row.Cells[i].Text));

暂无
暂无

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

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