0

当我使用以下代码填充我的 Datagridview 时:

Private Sub FormatGridView()
    Dim ILNumColumn As New DataGridViewTextBoxColumn
    Dim ArtNumColumn As New DataGridViewTextBoxColumn
    Dim DescColumn As New DataGridViewTextBoxColumn

    'Header text
    ILNumColumn.HeaderText = "# IL"
    ArtNumColumn.HeaderText = "# Articles"
    DescColumn.HeaderText = "Description"

    'Wrap
    DescColumn.DefaultCellStyle.WrapMode = DataGridViewTriState.True

    'Widths
    ILNumColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    ArtNumColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    DescColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

    'Add columns
    dgvArticles.Columns.Add(ILNumColumn)
    dgvArticles.Columns.Add(ArtNumColumn)
    dgvArticles.Columns.Add(DescColumn)
End Sub

我希望ILNumColumn其单元格中接受不超过 3 个数字。

我尝试了以下代码:

ILNumColumn.MaxInputLength = 3

它不起作用,我仍然可以写“9999”......一旦我到达“999”,那个代码不应该阻止我输入吗?我知道可以使用各种 datagridview 事件来做到这一点,但我想知道是否可以在将列添加到 datagridview 时做到这一点。

谢谢

4

1 回答 1

0

好吧,我找不到 MaxInputLength 的替代方法,所以我使用了CellValidating事件。

Private Sub dgvArticles_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvArticles.CellValidating
    'Ensures only numeric values are entered in these fields
    If e.ColumnIndex = 0 Then
        If e.FormattedValue <> "" And (e.FormattedValue < 0 Or e.FormattedValue >= 1000) Then
            MsgBox("IL # must be greater than 0 and less than 1000")
            e.Cancel = True
        End If
    End If
End Sub
于 2013-04-29T17:32:23.367 回答