单元格左移时自动格式化datagridview的单元格 [英] Auto formatting datagridview's cell on when cell is left

查看:74
本文介绍了单元格左移时自动格式化datagridview的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道每次进行一些更改时如何格式化2位小数的单元格。但是我只需要格式化精确的列,所以我认为这可能起作用:

Hello I'm wondering how to format cell for 2 decimal positions everytime there is made some change. But I need to format only exact column so I thought that this might work:

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
   dataGridView1.Columns[2].DefaultCellStyle.Format = "n2";
}

但是它不会格式化单元格。为什么?

But it doesn't format the cell. Why?

推荐答案

您需要包括 ValueType 属性以进行格式化正确,因为默认情况下它是 String 数据类型。假设您要将其设置为十进制数据类型,则将具有以下内容。

You need to include ValueType property in order to format it properly because by default it is a String data type. Say you want to make it Decimal data type then you will have the following.

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
  dataGridView1.Columns[2].DefaultCellStyle.Format = "N2";
  dataGridView1.Columns[2].ValueType = typeof(Decimal);
}

这篇关于单元格左移时自动格式化datagridview的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆