Windows Develop Bookmark and Share   
 index > Windows Forms General > Change the phone number format in datagrid view.
 

Change the phone number format in datagrid view.

Hi,

I work on a winform application.

I have a datagridview which has a column "Phone".
I want to format it so that it appears 123-456-7890 format. As of now the hyphens are not there.

How can i do it?
I found some solution online which goes like this:
string.Format("({0}) {1}-{2}",
phoneNumber.Substring(0, 3),
phoneNumber.Substring(3, 3),
phoneNumber.Substring(6));

But, the problem here is, i have to select the column in the gridview programmatically. How can i do this?


Is there any other way to set the phone number format?

Thanks in Advance

rowter  Wednesday, September 09, 2009 6:11 PM

string columnName = "Column Name";
string formattedValue=string.Empty;
for(int i=0;i<datagridview.Rows.Count;i++)
{
formattedValue = datagridview.Rows[i].Cells[columnName].Value;
formattedValue = string.Format("({0}) {1}-{2}",
formattedValue.Substring(0, 3),
formattedValue.Substring(3, 3),
formattedValue.Substring(6));
datagridview.Rows[i].Cells[columnName].Value = formattedValue;
}

Is this what you wanted???

- Paras

  • Marked As Answer byrowter Wednesday, September 09, 2009 7:58 PM
  •  
paras kumar  Wednesday, September 09, 2009 6:55 PM
I tired something like this:

dgvLookupMaintenance.CurrentRow.SetValues()

'If currrow = dgvLookupMaintenance.CurrentRow.ToString Then

For i = 0 To colcollection.Count

If dgvLookupMaintenance.Columns(i).Name = "Phone" Then

Dim strpnum As String = dgvLookupMaintenance.CurrentCell.Value

dgvLookupMaintenance.CurrentCell.Value = String.Format("({0}) {1}-{2}", strpnum.Substring(0, 3), strpnum.Substring(3, 3), strpnum.Substring(6))

Exit For

End If

Next


let me check with your code

thanks

  • Edited byrowter Wednesday, September 09, 2009 7:59 PM
  • Marked As Answer byrowter Wednesday, September 09, 2009 7:59 PM
  •  
rowter  Wednesday, September 09, 2009 7:07 PM

string columnName = "Column Name";
string formattedValue=string.Empty;
for(int i=0;i<datagridview.Rows.Count;i++)
{
formattedValue = datagridview.Rows[i].Cells[columnName].Value;
formattedValue = string.Format("({0}) {1}-{2}",
formattedValue.Substring(0, 3),
formattedValue.Substring(3, 3),
formattedValue.Substring(6));
datagridview.Rows[i].Cells[columnName].Value = formattedValue;
}

Is this what you wanted???

- Paras

  • Marked As Answer byrowter Wednesday, September 09, 2009 7:58 PM
  •  
paras kumar  Wednesday, September 09, 2009 6:55 PM
I tired something like this:

dgvLookupMaintenance.CurrentRow.SetValues()

'If currrow = dgvLookupMaintenance.CurrentRow.ToString Then

For i = 0 To colcollection.Count

If dgvLookupMaintenance.Columns(i).Name = "Phone" Then

Dim strpnum As String = dgvLookupMaintenance.CurrentCell.Value

dgvLookupMaintenance.CurrentCell.Value = String.Format("({0}) {1}-{2}", strpnum.Substring(0, 3), strpnum.Substring(3, 3), strpnum.Substring(6))

Exit For

End If

Next


let me check with your code

thanks

  • Edited byrowter Wednesday, September 09, 2009 7:59 PM
  • Marked As Answer byrowter Wednesday, September 09, 2009 7:59 PM
  •  
rowter  Wednesday, September 09, 2009 7:07 PM
do you want like 990-860-0061 or like (990)860-0061?
the code above (string.Format) formats phone number like (990)860-0061

- Paras
  • Marked As Answer byrowter Wednesday, September 09, 2009 7:58 PM
  • Unmarked As Answer byrowter Wednesday, September 09, 2009 7:58 PM
  •  
paras kumar  Wednesday, September 09, 2009 7:26 PM
Either way is good enough.

I got it working.

Thanks paras
rowter  Wednesday, September 09, 2009 7:58 PM

You can use google to search for other answers

Custom Search

More Threads

• how can I lose the caret in the Combobox at runtime.
• creating a stand alone .exe
• Return a value from windows Form
• WebBrowser Empty Document
• How do i deploy my application on client computer
• Fire events outside the form (KeyPress)
• Leave & lostFocus event of usercontrol
• SelectNextControl inner workings
• Validating event when closing form
• ListView not allowing me to reference by key/name. Null reference error.