我的datagridview列中有货币文本和数字类型值。我想使用下面的代码格式化货币值,但它不起作用。
private void gv_EmployeeAdditional_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (gv_EmployeeAdditional.Rows[e.RowIndex].Cells["EmpDatatype"].Value.ToString() == "Currency")
{
gv_EmployeeAdditional[e.ColumnIndex, e.RowIndex-1].Style.Format = "#,##0.00##";
}
}
答案 0 :(得分:0)
现在无法测试它,但我认为这就是你要找的,设置一个断点并确保你进入if语句然后运行一些测试用例...
if (gv_EmployeeAdditional.Rows[e.RowIndex].Cells["EmpDatatype"].Value.ToString() == "Currency")
{
gv_EmployeeAdditional.Columns[e.ColumnIndex].DefaultCellStyle.Format = "N2";
gv_EmployeeAdditional.Columns[e.ColumnIndex].ValueType = typeof(decimal);
//gv_EmployeeAdditional[e.ColumnIndex, e.RowIndex - 1].Style.Format = "#,##0.00##";
}
也许只是尝试设置这一行:
gv_EmployeeAdditional[e.ColumnIndex, e.RowIndex - 1].Style.Format = "N2";
在C#link here
中格式化类型