Frage

When I populate my Datagridview using the following code:

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

I want the ILNumColumn to accept no more than 3 numbers in its cell.

I have tried the following code:

ILNumColumn.MaxInputLength = 3

It doesn't work, I can still write "9999" ... Shouldn't that code stop me from typing once I get to "999" ? I know it is possible to do it using various datagridview events but I'm wondering if it is possible to do that upon adding the column to the datagridview.

Thank you

War es hilfreich?

Lösung

Well, I wasn't able to find an alternative to MaxInputLength so I used a CellValidating event.

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top