vb.net - datagridview - 格式不起作用
情况:
我有一个带有一列(col1)的datagridview。
在数据网格定义(设计视图)中,我创建了这一行,并将格式设置为 C2(或 C0,或 C - 我都尝试过)。
然后,我以编程方式加载数据网格视图:
dbl_myValue as double
dbl_myValue = 6.99
datagridview1.item(0,0) = dbl_myValue
datagridView 正确显示数据:6.99$
当我“监视”datagridview.item(0,0) 时,“Value”属性是a Double。
问题:
如果用户手动输入值,则不起作用:单击单元格,输入一个数值(比方说 100 ,不带小数以避免复杂性),然后离开单元格(因此不再处于编辑模式)。 格式不适用。
当我“监视” datagridview.item(0,0) 时,“value”属性是一个 STRING。
所以我猜这就是问题所在,但我能做什么呢?
我尝试进入 CellValidating 并用如下代码填充它:
Private Sub DataGridView1_CellValidating(....)
Dim c As Control = DataGridView_WorkOrderAddition.EditingControl
c.text = formatNumeric(c.text)
c.text = convert.toDouble(c.text)
但它仍然不起作用。
The situation:
I have a datagridview with one column (col1).
In the datagrid definition (design view), i have created this row, and set the formatting to C2 (or C0, or C - I tried them all).
then, I load my datagrid view programmatically :
dbl_myValue as double
dbl_myValue = 6.99
datagridview1.item(0,0) = dbl_myValue
The datagridView shows the data correcly : 6.99$
When I "spy" on the datagridview.item(0,0), the "Value" property is a Double.
the problem :
It doesn't work if the user manually enter a value : click in the cell, enter a numeric value (let's say 100, without decimal to avoid complexity) and then leave the cell (so no more in edit mode).
The formating just dont applies.
When I "spy" on the datagridview.item(0,0), the "value" property is a STRING.
So I guess this is the problem, but what can I do?
I've tried to go in CellValidating and stuffing it with code like:
Private Sub DataGridView1_CellValidating(....)
Dim c As Control = DataGridView_WorkOrderAddition.EditingControl
c.text = formatNumeric(c.text)
c.text = convert.toDouble(c.text)
But it still doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,当 DGV 未绑定时,单元格默认存储字符串。设置列的 CellType 属性可以解决这个问题,Decimal 最适合处理货币值。您还需要实现 DataError 事件,以便警告用户字符串无法转换。因此:
Yes, when the DGV is not bound then cells default to storing strings. Setting the column's CellType property can solve that, Decimal is most appropriate for handling money values. You'll also need to implement the DataError event so you can warn the user that the string cannot be converted. Thus: