michael24B
michael24B

Reputation: 313

ASP.NET C# BoundField format (Remove leading zeros)

How should DataFormatString of BoundField in Gridview look like that values won't have leading zeros?

So far I got this:

 <asp:BoundField DataField="NUMBER" HeaderText="Id. number" DataFormatString="{0:d}">

Expected result:

000001 -> 1

002101 ->2101

I tried to figure that problem out with official documentation and this page. So far unsuccessful.

Upvotes: 0

Views: 2718

Answers (4)

AdamRoof
AdamRoof

Reputation: 274

Simplifiying A Kimmels answer, no need to place in a server label, no concern of single or double quotes, no concern of underlying datatype, as it casts the object to string, then performs a string.TrimStart, no need for codebehind

<asp:TemplateField HeaderText="Work Order">
    <ItemTemplate>
        <%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>
    </ItemTemplate>
</asp:TemplateField>

0123456 = 123456

0012345 = 12345

if a server label is required, just start and end the text tag with single quotes

<asp:Label ID="lblWorkOrder" runat="server" Text='<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>' />

Upvotes: 1

A Kimmel
A Kimmel

Reputation: 176

Another approach is to use String.Trim function. The following code is how I accomplished what you are trying to do:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label runat="server" Text="<%#Eval(&quot;NUMBER&quot;).ToString().TrimStart('0')%>" ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

The tricky part was using the correct combination of quotes and single quotes. You can use

&quot;

in place of the quote character around your datafield column that you are displaying.

Upvotes: 1

Humpy
Humpy

Reputation: 2012

When you want to format something properly in a boundfield, I always suggest converting it to a templatefield. It's much easier to work with than boundfields. Here is an example of how the templatefield will look like once it's converted.

<asp:TemplateField ShowHeader="False" Visible="False">
       <EditItemTemplate>
          <asp:TextBox ID="tbEditNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
           <asp:TextBox ID="tbNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
      </ItemTemplate>
</asp:TemplateField>

within that template field I placed..

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

This should format it into a number and should drop the leading zeros.

EDIT: You might be able to try

Text='<%# String.Format("{0:n}", Eval("Number") ) %>'

Upvotes: 2

codeandcloud
codeandcloud

Reputation: 55250

Try this. Much cleaner this way.

Markup

<asp:TemplateField HeaderText="Id. number">
        <EditItemTemplate>
            <asp:TextBox ID="tbEditNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:TextBox ID="tbNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
        </ItemTemplate>
</asp:TemplateField>

Code-behind

protected int ConvertToDigit(object oNum)
{
    var i = 0;
    if (oNum != null) int.TryParse(oNum.ToString(), out i);
    return i;
}

Upvotes: 0

Related Questions