Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > I cannot change the number format in DataGridView
 

I cannot change the number format in DataGridView

I have the following code. When I use:

Fori = 3ToMaxColumnDataGridView

DataGridView.Columns(i).DefaultCellStyle.NullValue = 0

DataGridView.Columns(i).DefaultCellStyle.Format ="N5"

Next

the columns format can’t be changed. I cannot change the number format whatever I do. How can I solve this problem?

iv_ski  Monday, November 30, 2009 11:40 AM
Did you try "0" for default
and
"0.00000" for format?




Success
Cor
Cor Ligthert  Monday, November 30, 2009 11:48 AM
Yes, but no success.
iv_ski  Monday, November 30, 2009 12:57 PM
Yes, no success.
iv_ski  Monday, November 30, 2009 3:05 PM
Sorry, it was already suggested by Cor

Not sure whats going through,
Are you fetching datatable from database with integer columns which you intend to change?

Is it possible to show some more code?

Does it work when you change those formats in designtime?

Arjun Paudel
Arjun Paudel  Monday, November 30, 2009 3:39 PM
OK. Here is the code:

Private Sub ChangeListViewFormat()

Dim i As Short

For i = 3 To MaxColumnListView

ListView.Columns(i).DefaultCellStyle.Format =

"0,00000"

ListView.Columns(i).DefaultCellStyle.NullValue =

"0"

Next

End Sub

Private Sub frmSimParam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim Row As UInt16

LoadDatamap()

For Row = 0 To ListView.RowCount - 1

ChangeListView(Row, ListView.Rows(Row).Cells(2).Value, Color.FromArgb(255, 236, 233, 216))

Next

ChangeListViewFormat()

End Sub

LoadDatamap is fetching datatable fromASCII filewith integer columns whichI intend to change.

iv_ski  Monday, November 30, 2009 4:32 PM
Following is my simple test with text file and its fine

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""d:\temp\"";" & _
            "Extended Properties=""text;HDR=Yes;FMT=Delimited""")

        Dim adapter As New OleDb.OleDbDataAdapter("SELECT * FROM  data.txt", connection)
        Dim dt As New DataTable
        DataGridView.DataSource = dt
        adapter.Fill(dt)
        FormatColumn()
    End Sub

    Private Sub FormatColumn()
        Dim maxcolumns = 9
        For i As Integer = 3 To maxcolumns - 1
            DataGridView.Columns(i).DefaultCellStyle.NullValue = "0"
            DataGridView.Columns(i).DefaultCellStyle.Format = "N5"
        Next
    End Sub

Text File is as follow (in d:\temp\data.txt)

FName,LName,Address,First,Second,Third,Fourth,Fifth,Sixth
Mark,Jude,TPS,1,2,3,4,5,6
Tony,Blayer,EL,1,6,9,4,5,


Arjun Paudel
  • Proposed As Answer byMalange 9 hours 50 minutes ago
  •  
Arjun Paudel  Monday, November 30, 2009 5:05 PM
Many thanks.
iv_ski  20 hours 47 minutes ago
Many thanks.
can propose as answer by Arjum Please. so we can move on
Don't judge me, just Upgrade me. Thanks!
Malange  9 hours 49 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• T-SQL in my VB.NET code!
• Time notation
• Input string was not in a correct format.
• What's the best secure way to get info from database?
• Serialization
• Fire SelectedIndexChanged event programatically in combo box of WebBrowser Control
• Toggle effect on user link or button
• Convert From C# Generics to VB.net
• Delete in gridview
• How to a long line into 2 lines?