[英]ASP.NET C# BoundField format (Remove leading zeros)
How should DataFormatString of BoundField in Gridview look like that values won't have leading zeros? Gridview中BoundField的DataFormatString应该如何看起来像值不带前导零?
So far I got this: 到目前为止,我得到了:
<asp:BoundField DataField="NUMBER" HeaderText="Id. number" DataFormatString="{0:d}">
Expected result: 预期结果:
000001 -> 1 000001-> 1
002101 ->2101 002101-> 2101
I tried to figure that problem out with official documentation and this page . 我试图通过官方文档和此页面解决该问题。 So far unsuccessful.
到目前为止没有成功。
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.
这是templatefield转换后的外观示例。
<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}") %>'
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") ) %>'
Text ='<%#String.Format(“ {0:n}”,Eval(“ Number”))%>'
Another approach is to use String.Trim function. 另一种方法是使用String.Trim函数。 The following code is how I accomplished what you are trying to do:
以下代码是我如何完成您想要做的事情:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text="<%#Eval("NUMBER").ToString().TrimStart('0')%>" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The tricky part was using the correct combination of quotes and single quotes. 棘手的部分是使用引号和单引号的正确组合。 You can use
您可以使用
"
in place of the quote character around your datafield column that you are displaying. 代替要显示的数据字段列周围的引号字符。
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;
}
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 简化Kimmels答案,无需将其放置在服务器标签中,无需关注单引号或双引号,无需关注基础数据类型,因为它将对象转换为字符串,然后执行字符串.TrimStart,无需代码隐藏
<asp:TemplateField HeaderText="Work Order">
<ItemTemplate>
<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>
</ItemTemplate>
</asp:TemplateField>
0123456 = 123456 0123456 = 123456
0012345 = 12345 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()) %>' />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.