您好,我想知道每次进行一些更改时如何格式化2位小数的单元格。但是我只需要格式化确切的列,所以我认为这可能起作用:
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[2].DefaultCellStyle.Format = "n2";
}
但是它不会格式化单元格。为什么?
您需要包括ValueType
属性以便正确格式化它,因为默认情况下它是一种String
数据类型。假设您要使其为Decimal
数据类型,则将具有以下内容。
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[2].DefaultCellStyle.Format = "N2";
dataGridView1.Columns[2].ValueType = typeof(Decimal);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句