how to mask cell in datagridview

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a datagridview control that I load via the program from a stored procedure. I got the grid set up without problem except for one item that being formatting cell data. In the data row I have several phone numbers, cell, fax etc which I want to format/mask to display. I tried a number of attempts to use the cellformatting event and while I get no compile error, the code doesn't work.
Does someone have some sample code I could use to mask the phone numbers for display on the grid?

Thanks,
Tom

ps current non working code:
If Me.dgVendors.Columns(e.ColumnIndex).Name = "VendorPhone" Then
If e.Value IsNot Nothing Then
dgVendors.Columns(e.ColumnIndex).DefaultCellStyle.Format = "(###) ###-####"
End If
End If
 
VB.NET:
Public Class Form1

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

		Dim dt As New DataTable

		dt.Columns.Add("ID", GetType(Integer)).AutoIncrement = True
		dt.Columns.Add("Phone", GetType(Int64)) 'Works
		dt.Columns.Add("PhoneStr", GetType(String)) 'Doesn't work

		With dt.Rows
			.Add(Nothing, 1112223333, "1112223333")
			.Add(Nothing, 2223334444, "2223334444")
			.Add(Nothing, 3334445555, "3334445555")
			.Add(Nothing, 4445556666, "4445556666")
		End With

		Me.DataGridView1.DataSource = dt

	End Sub

	Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
	Handles DataGridView1.CellFormatting

		If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Phone" Then
			Me.DataGridView1.Columns(e.ColumnIndex).DefaultCellStyle.Format = "(###) ###-####"
		End If

		If Me.DataGridView1.Columns(e.ColumnIndex).Name = "PhoneStr" Then
			Me.DataGridView1.Columns(e.ColumnIndex).DefaultCellStyle.Format = "(###) ###-####"
		End If

	End Sub
End Class
 
Note also that you would normally set DefaultCellStyle for a column in designer properties, or once in Load event perhaps - and not in CellFormatting event.
help said:
The CellFormatting event occurs every time each cell is painted
 
Back
Top