Home>

I just started to touch VB.NET.
I would like to know why this happens.

[]
Display the value surrendered as a String as a String

[Current]
The value surrendered as a String is displayed as an Integer

Dim dt As DataTable 'DataGridView content is already stored in dt
'"0000000001"
Dim CustomerCode As String = String.Format ("{0: D10}", 1)
'1 (Integer)
dt.Rows (0) ("CUSTOMERCODE") = CustomerCode

As mentioned above, it should be entered as the string type "0000000001", but it will actually be entered as the numeric type "1".

Dim dt As DataTable 'DataGridView content is already stored in dt
'"000000001_A"
CustomerCode = String.Format ("{0: D10}", 1)&"_A"


If i do the above, you will end up with a type error.

When information brought from DB is set in DataGridView once

The above is the one that was entered manually into the empty grid from the beginning.
DB → DataGridView → DataTable → DB
When processing in the order of
Does the data that should have been surrendered by String become Integer because the column manually edited when bringing information from DB is Integer type?
If all of the grid's manually entered data is only numbers, will it be considered an Integer type, and will zero-filled data in String.Format be converted to Integer?

  • Answer # 1

    It seems that the column type of DataTable is Integer type. A String type should be as expected. Look at the image below.

  • Answer # 2

    option strict off Isn't it?

  • Answer # 3

    What is the type of CUSTOMERCODE column in DataTable?
    Look at what dt.Columns (CUSTOMERCODE) .DataType looks like.
    (Or you can check the type notation with a string if you use dt.Columns (CUSTOMERCODE) .DataType.ToString.)
    If it is a numeric type, it is treated as a numeric value, and the zero padding is also removed.

      

    If you do the above, it will be thrown back as a type error.

    means that the column type is a numeric system such as Integer.
    To avoid this,
    -When getting the original DataTable, it becomes the formatted String type.
    -Format the target column in DataGridView.
    Reference: Specify the format (format) of the text to be displayed in the DataGridView cell
    https://dobon.net/vb/dotnet/datagridview/format.html

    However, if you think that the target column of the original DB is a numeric value and the value edited with the grid is returned to the DB, it is better to format the target column with the latter DataGridView think.

Trends