【问题标题】:Limit datagridview to numeric only vb.net将datagridview限制为仅数字vb.net
【发布时间】:2017-04-13 18:08:18
【问题描述】:

我在 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

【问题讨论】:

  • 那么您是希望在键入时限制这些列中的字符,还是希望通过验证来检查编辑的单元格是否有效?
  • this question 的前两个答案应该会有所帮助。
  • 列数据源的底层数据类型将为您完成此操作
  • 这是 vb.net 中的未绑定数据网格视图 我认为该链接适用于 C++。我不在乎方法。当VB忽略该属性时,为什么会有这个属性?当数据源未绑定时,如何使用不被视为字符串的数字填充单元格?我正在从文本文件加载数据。

标签: vb.net datagridview


【解决方案1】:

如果我们正在根据您现有的内容进行构建,而您只想将其更改为小数点后 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

请注意,如果存在第四位,则此答案将四舍五入小数点后第三位。

【讨论】:

    【解决方案2】:

    我找到了它并且它有效。但是如何将数字限制为小数点后 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 = ""
            End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多