Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:47:54+00:00 2026-05-16T04:47:54+00:00

I see another thread somewhat like my question at: ASP.NET GridView Column – formatting

  • 0

I see another thread somewhat like my question at:

ASP.NET GridView Column – formatting telephone number

but i do not know if it answers my question as he is using code-behind to make the colum. All I did was insert the GridView control in visual studio. BTW, the data is being populated in the Grid, I am just trying to get the formatting set now.

I am using Microsoft Visual Studio Professional 2010. (Also SQL Management Studio for my database but this information may not be needed, just trying to give enough to make sure what i am doing is understood)

I am making a website in ASP.NET with Visual Basic.net code behind.

The site is basically a contact list site.

3 Text Box Fields. First Name, Last Name, Main Phone #.

Add Record Button (Takes the information from the text boxes and inserts into a database)

GridView that shows the database that is being populated with the information

I have a “Main Phone Number” Column and this pulls a telephone number to show in GridView. The number is only 10 digits, no formatting…(i.e. 999-999-9999)

I am trying to make GridView take the 9999999999 and make it (999) 999-9999

If I look at the DataFormatString I have tried many combinations of “{0:(###) ###-####}” with and without the quotations and also with all zeroes instead of pound signs.

Through my research it seemed that if I want to use DataFormatString I need to make my phone number in my database an int. So I deleted my table and re-created it from a varchar to an int. I get to the DataFormatString by clicking on Gridview Tasks (arrow at top right of GridView)… then “Edit columns”… then under “Selected Fields” I click the name of the column… “Main Phone Number” then on the under “CommandField properties” I scroll down to “DataFormatString”.

I hope I am not being too detailed. I have really appreciated all the help.

I found this:

http://www.tek-tips.com/viewthread.cfm?qid=328173

but i don’t know how i would go about using it.. seeing as how because my code was done by Visual Studio… some of it looks like this


UPDATE: I posted the wrong code originally, either way, based off what I stated Kelsey was able to suggest an answer for me that worked.

Below is my new code WITH the corrections that Kelly gave.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="EmpId" DataSourceID="SqlDataSource1" 
            EmptyDataText="There are no data records to display." CellPadding="4" 
        ForeColor="#333333" GridLines="None" Height="136px" Width="299px" 
              AllowSorting="True">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True" 
                    SortExpression="EmpId" Visible="False" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                    SortExpression="LastName" />
<%--                <asp:BoundField DataField="MainPhoneNumber" HeaderText="Main Phone Number" 
                    SortExpression="MainPhoneNumber" />--%>
                    <asp:TemplateField HeaderText="Main Phone Number"> 
                <ItemTemplate> 
                 <asp:Literal ID="litPhone"  runat="server" Text='<%# string.Format("{0:(###) ###-####}", Int64.Parse(Eval("MainPhoneNumber").ToString())) %>' /> 
                </ItemTemplate> 
                </asp:TemplateField> 

            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:47:55+00:00Added an answer on May 16, 2026 at 4:47 am

    Since you didn’t post your GridView code I have to assume that your columns are using a BoundField like this or something similar:

    <Columns>
        <asp:BoundField DataField="MainPhoneNumber" DataFormatString="{0:(###) ###-####}" />
    

    Since it is a string, you can’t use the DataFormatString property so you will need to change your BoundField to a TemplateField. Just replace your BoundField with the following TemplateField and it should work:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal ID="litPhone" runat="server" Text='<%# string.Format("{0:(###) ###-####}", Int64.Parse(Eval("MainPhoneNumber").ToString())) %>' />
        </ItemTemplate>
    </asp:TemplateField>
    

    The key here is the code that evaluates the databound field and converts it to an Int64 so that the string formatter will work. Note that I am using an Int64 and not just an int which is 32 bit because a 10 digit number will not fit.

    I have tested it and it works 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see another question on stackoverflow.com whose title seems similar but that doesnot fulfil
This question stems off another post I had. (see Search through column in excel
In ASP.Net a request can migrate from one thread to another (thread agility) :
Answering another thread (see stackoverflow: generate css color schemes ) I bumped into the
I'm having trouble with memory in a j2me application. (see another question ) I
I would like to append one DataTable to another DataTable. I see the DataTable
UPDATE : I asked this question in another form (see below), and it got
This question is a continuation of my question in another thread .Since the point
At work I see one colleague designing a site in Photoshop/Fireworks, I see another
Why one project (exe) does not see the namespace of another project (dll) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.