开发者

vb.net - datagridview - Formatting is not working

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 datagr开发者_如何学JAVAidView 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜