0

I have a problem with the GridView column formatting. I have a PhoneNumber that is currently coming from database as (###)###-##### but I want the format to be ############ without spacing and brackets. I have tried every thing like DataFormatString="{0:###-####}" or than converting it into TemplateField and giving it format but not working.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
  <Columns>
    <asp:TemplateField HeaderText="Phone Number">
      <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# 
            Bind("PhoneNumber") %>'></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" 
            Text='<%# Bind("PhoneNumber", "{0:d}") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

This is before converting the column to TemplateField:

 <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number Home" 
     SortExpression="Phone" DataFormatString="{0:######-####}" HtmlEncode="false" />

But nothing is working. I still get the format of phone number like this (123) 123-4567.

3 Answers 3

1

Try to replace Text property in TemplateField:

Text='<%# Bind("PhoneNumber", "{0:d}") %>'

with:

Text='<%# String.Format("{0:##########}", 
                  Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PhoneNumber")))%>'

Use "#" Custom Specifier.

Sign up to request clarification or add additional context in comments.

9 Comments

Input string was not in a correct format. i keep getting this Error i even tried other Formats it doesn't fox it self .
Exception Details: System.FormatException: Input string was not in a correct format.
I have edit my answer with a reference to custom format specifier.
' <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# String.Format("{0:F2}", Convert.ToInt64(DataBinder.Eval(Container.DataItem, "LocalPhone")))%>' > </asp:TextBox>'
This is how it looks like with # thing in front but it still giving me error.
|
0

Formatting is solely additive, not subtractive. To accomplish this, you would need to expose a version of the values which has non-numerics stripped out, so that your client-side formatting can act upon it.

2 Comments

Non-numerics Stripped out ? u mean the Brackets stripped out yes but how ? what code should i use
@Haseeb.A When you retrieve the phone number from your datasource, you should either strip the non-numeric characters out and treat it as a string, or strip out the non-numeric characters and treat it as an integer. In either scenario you would then be able to apply formatting. There are plenty of answers on how to strip out values in C#.
0

If you are dynamically binding the rows in code behind, you should be formatting the columns in the DataBind event of the gridview.

The semantical column formatting works only when you use bind data semantically, for example declare

1 Comment

I didn't quite understand that how i even tried onRoDataBound

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.