I guess nobody know formatting in the DataGridView?????????????????????

Z

Zim Babwe

I have an unbound DataGrieView on a Windows form (VS 2005) and I have added
a column to a DataGridView

Dim textCol As New DataGridViewTextBoxColumn

textCol.Name = "ContactPhone1"

dgcontacts.Columns.Insert(.Columns.Count, textCol)

dgcontacts.Columns("ContactPhone1").DefaultCellStyle.Format = "(000)
000-0000"



When the grid is displayed on the form, there is no formatting in the cell.
What am I doing wrong and How can I fix it?



Thanks
 
C

ClayB

One way to do special mask type formatting is to subscribe to the
CellFormatting event and do it there.

Dim o As Object = e.Value
If Not (o Is Nothing) And e.ColumnIndex = 1 And
e.Value.ToString().Length = 10 Then
Dim s As String = o.ToString()
e.Value = String.Format("({0}){1}-{2}", s.Substring(0, 3),
s.Substring(3, 3), s.Substring(6))
e.FormattingApplied = True
End If

====================
Clay Burch
Syncfusion, Inc.
 
J

Jim Wooley

Hello Zim,
dgcontacts.Columns("ContactPhone1").DefaultCellStyle.Format = "(000)
000-0000"

When the grid is displayed on the form, there is no formatting in the
cell. What am I doing wrong and How can I fix it?

What is the type of the ContactPhone? The format string should work for a
number, but if this is a string, I think it will just display the string.

dim phone as string = "1234567890"
Console.WriteLine(String.Format(phone, "(000) 000-0000") 'outputs "1234567890"
Console.WriteLine(CInt(phone).ToString("000) 000-0000") 'outputs "(123)
456-7890"

Jim Wooley
http://devauthority.com/blogs/jwooley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top