Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net c中为动态gridview单元格设置货币#_C#_Asp.net_C# 4.0_Gridview_Aspxgridview - Fatal编程技术网

C# 在asp.net c中为动态gridview单元格设置货币#

C# 在asp.net c中为动态gridview单元格设置货币#,c#,asp.net,c#-4.0,gridview,aspxgridview,C#,Asp.net,C# 4.0,Gridview,Aspxgridview,我有一个动态gridview,需要对某些列应用格式设置 代码: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { for (int i = 0; i < GridView1.HeaderRow.Cells.Count;

我有一个动态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);
                }
            }
        }
    } 
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
对于(int i=0;istrHeader.Equals(o));
如果(支票金额)
{
e、 Row.Cells[i].Text=String.Format(“{0:C2}”,e.Row.Cells[i].Text);
}
}
}
} 
如果gridview列标题文本包含
价格
金额
,则需要应用货币格式,且上述代码不起作用


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

您是否尝试过解析该值?根据您的要求使用Int或Decimal

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

你试过解析这个值吗?根据您的要求使用Int或Decimal

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

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

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

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

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

但它不会更改所有行的格式。例如:如果我有200行,格式只适用于少数几行。@vicky:可能有很多原因造成这种情况。最好在
内设置断点,以便
循环检查。例如,
strHeader
可以是小写的“price”,等等,但它不会更改所有行的格式。例如:如果我有200行,格式只适用于少数几行。@vicky:可能有很多原因造成这种情况。最好在
内设置断点,以便
循环检查。例如,
strHeader
可以是小写的“price”等。