Csharp/C#教程:当单元格离开时自动格式化datagridview的单元格分享


当单元格离开时自动格式化datagridview的单元格

您好我想知道如何在每次进行一些更改时格式化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); } 

试试吧 ..

 dataGridView1.Rows[0].Cells[2].Value=double.Parse(dataGridView1.Rows[0].Cells[2].Value.ToString()).ToString("F",System.Globalization.CultureInfo.InvariantCulture); 

从这里

上述就是C#学习教程:当单元格离开时自动格式化datagridview的单元格分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1010113.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐