我需要在DataGridView
中显示十进制值 if (dgvClients.SelectedRows.Count != 0)
{
DataGridViewRow row = dgvClients.SelectedRows[0];
row.Cells["NetAccountValue"].Value = info.DecimalValue;
}
我需要在小数点分隔符后将值格式化为给定的数字。
问题是单元格Value
属性在我的情况下存储对十进制的引用。显示decimal.ToString()时,默认decimal.ToString()生成无格式字符串
有很多数字。
一种方法是创建字符串并使用String.FormatString()来实现所需的格式 并将其作为值而非十进制。
还有其他方法吗?
答案 0 :(得分:1)
使用代码,您可以设置DataGridView.DefaultCellStyle property。
您也可以通过以下方式在设计师中执行此操作:
答案 1 :(得分:0)
我发现这个解决方案很简单。我已经搜索了两天,终于找到了。 - 您的db列类型必须是Money或number,double,single。 - 更新datagridview事件“CellFormatting”,如下面的代码。
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView2.Columns[e.ColumnIndex].Name == "Aidat")
{
string deger=(string)e.Value;
deger = String.Format("{0:0.00}", deger);
}
}