将datagridview限制为仅限数字vb.net

时间:2017-04-13 18:08:19

标签: vb.net datagridview

我在vb.net的windows窗体上有一个datagridview。在一列或多列中,我希望文本仅限于数字,包括负号和一个小数点。我有列属性"格式"设置为3位数字,但我仍然可以输入我想要的任何文字。那么让VB使用该属性的诀窍是什么?

我使用它时没有改变行为。 https://msdn.microsoft.com/en-us/library/f9x2790s(v=vs.110).aspx

我也尝试了这一点而没有改变行为。

DataGridView1.Columns(2).DefaultCellStyle.Format = "N3"

从这里: How to format numbers to 3 decimal place in datagridview using vb.net

2 个答案:

答案 0 :(得分:0)

我发现了这个并且它有效。但是如何将数字限制为3位小数?

<div><span>HUMPTY DUMPTY S</span><span class="centered">A</span><span>T</span></div>

<p>center</p>

答案 1 :(得分:0)

如果我们依靠您所拥有的东西,并且您只想将其更改为3位小数,请尝试以下操作:

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    If Not IsNumeric(DataGridView1(3, e.RowIndex).Value) Then
        DataGridView1(3, e.RowIndex).Value = ""
    Else
        DataGridView1(3, e.RowIndex).Value = Decimal.Parse(DataGridView1(3, e.RowIndex).Value).ToString("N3")
    End If
End Sub

请注意,如果第四个小数位存在,此答案将围绕小数点后三位。

相关问题