vb.net - datagridview - 格式不起作用

发布于 2024-10-03 01:56:44 字数 898 浏览 1 评论 0原文

情况

我有一个带有一列(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-10-10 01:56:44

是的,当 DGV 未绑定时,单元格默认存储字符串。设置列的 CellType 属性可以解决这个问题,Decimal 最适合处理货币值。您还需要实现 DataError 事件,以便警告用户字符串无法转换。因此:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        DataGridView1.Columns(0).ValueType = GetType(Decimal)
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub
End Class

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:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        DataGridView1.Columns(0).ValueType = GetType(Decimal)
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文