Windows Develop Bookmark and Share   
 index > Windows Forms General > datagridview cell formatting - help
 

datagridview cell formatting - help

I have what I believe to be a simple unbound datagridview in a VB 2005 Beta 2 windows application.  The issue is that I can not get numeric data to be formatted in the cells as "12,345" instead of "12345".  Does anyone know what is wrong with the following code (this is the entire set of code in the project) - I must be missing something pretty simple.  The grid is the only control on the form.  Specifically the statement <columnStyleR.Format = "#,##0"> does not seem to work.

Thanks!



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

dgvA.AllowUserToAddRows = False

dgvA.AllowUserToDeleteRows = False

dgvA.AllowUserToOrderColumns = True

dgvA.ReadOnly = True

dgvA.RowHeadersVisible = False

dgvA.ColumnHeadersVisible = True

dgvA.SelectionMode = DataGridViewSelectionMode.FullRowSelect

dgvA.MultiSelect = True

dgvA.ColumnCount = 2

Dim columnStyleR As New DataGridViewCellStyle()

columnStyleR.BackColor = Color.Yellow

columnStyleR.Alignment = DataGridViewContentAlignment.TopRight

columnStyleR.Format = "#,##0"

dgvA.Columns(0).Name = "Index#"

dgvA.Columns(0).HeaderText = "Index#"

dgvA.Columns(0).MinimumWidth = 25

dgvA.Columns(0).Width = 40

dgvA.Columns(0).DefaultCellStyle = columnStyleR

dgvA.Columns(1).Name = "Size"

dgvA.Columns(1).HeaderText = "Size"

dgvA.Columns(1).MinimumWidth = 50

dgvA.Columns(1).Width = 75

dgvA.Columns(1).DefaultCellStyle = columnStyleR

Dim row0 As String() = {1, 12345}

Dim row1 As String() = {2, -12345}

Dim row2 As String() = {3, 1234.567}

dgvA.Rows.Add(row0)

dgvA.Rows.Add(row1)

dgvA.Rows.Add(row2)

End Sub

Matty4242  Wednesday, September 21, 2005 2:08 PM

By submitting this as a bug to MS I got the answer to my dilemma, so I will post it here in case anyone else has the same problem.  here's the link:

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b3d80d70-efcf-4d68-bb61-35070912e2af

Matty4242  Wednesday, September 21, 2005 9:36 PM
You have a numeric format, but you've set the cells to string values.  This worked for me:

dgvA.ColumnCount = 2
dgvA.RowCount = 3
dgvA.Rows(0).Cells(0).Value = 1
dgvA.Rows(1).Cells(0).Value = 2
dgvA.Rows(2).Cells(0).Value = 3
dgvA.Rows(0).Cells(1).Value = 12345
dgvA.Rows(1).Cells(1).Value = -12345
dgvA.Rows(2).Cells(1).Value = 1234.456
   -Scott

Scott Berry  Wednesday, September 21, 2005 9:19 PM

By submitting this as a bug to MS I got the answer to my dilemma, so I will post it here in case anyone else has the same problem.  here's the link:

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b3d80d70-efcf-4d68-bb61-35070912e2af

Matty4242  Wednesday, September 21, 2005 9:36 PM

You can use google to search for other answers

Custom Search

More Threads

• form1 to form2 to form3 and return back to form 1.
• User setting font in a TextBox
• General question - InitializeComponent versus Form_Load
• ApplicationSettings problem
• wmp 6.0
• multiple definations with identical signatures
• Drag & Drop to Windows Explorer shell
• How to capture mouse click events outside my form using C#?
• Full screen is not full screen
• "Component Class" or "class"