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

  • Thread starter Thread starter Zim Babwe
  • Start date Start date
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
 
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.
 
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
 
Back
Top