Quantcast
Channel: Limno (Jingyang Li) » ASP.NET 2
Viewing all articles
Browse latest Browse all 10

Format string with GridView

$
0
0

One user at ASP.NEt forum asked a question for how to format a 15 digit number(string) with two hyphen (-) inside at position 10 and 15.

It can be don easily in SQL:

SELECT  LEFT(@From,9) +’-'+SUBSTRING(@From,10,5)+’-'+RIGHT(@From,1)

The same can be done at the front end with string manipulation.

For the data column in GridView, we need to convert the BoundField to TemplateField first and apply the string.Insert to Eval at the designated position twice.

Text=’<%# Eval(“rawCol”).ToString().Insert(9,”-”).Insert(15,”-”) %>’

We can see this in action below:

<asp:TemplateField HeaderText=”rawCol” SortExpression=”rawCol”>

<EditItemTemplate>

<asp:TextBox ID=”TextBox1″ runat=”server” Text=’<%# Bind(“rawCol”) %>’></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(“rawCol”).ToString().Insert(9,”-”).Insert(15,”-”) %>’></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField=”col” HeaderText=”col” ReadOnly=”True”

SortExpression=”col” />

 



Viewing all articles
Browse latest Browse all 10

Trending Articles