Datagridview Number Format

2019-08-30 18:42发布

问题:

I am trying to create a datagridview that will automatically format the numbers that a user enters. My datagridview is not linked to a datasource, it is built programmatically like so:

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

Is there a way to make the IlNumColumn always be in the format below in the green circle?

回答1:

Number format

'Formatting
ILNumColumn.DefaultCellStyle.Format = "000"

This will make all the numbers in the IlNumColumn in a "001" format.