Jump to content

Truncate\format a field?


aic007

Recommended Posts

HiI have a phone number field from sql that is shown in a ListView on the web, and has this format:555 555 55My requirement is to show the last 4 digits only. I use this code to show only the last four digits:string tlfnr = ((DataRowView)e.Item.DataItem)[1].ToString(); tlfnr = tlfnr.Trim(); cell2.Text = (tlfnr.Length > 4) ? tlfnr.Substring(tlfnr.Length - 4) : ""; The problem is that my code counts the blank space as a digit also. So my output is like this:5 55I want the ouput to be:5555Any ideas on how I can acheive that?Regards.

Link to comment
Share on other sites

HiThansk for answering. I fixed it another way:tlfnrtlfnr = tlfnr.Replace(" ", string.Empty).Trim(); But then I have another problem also. I would be most thankful if you could help me out on this one When I get the result, my list looks like this;name, phone nr.Bjørn Borg, 1234Bjør Arne Pedersen, 3456Asfandyar Wali Khan Karzai bin Laden 2356but I want it to be like this:Bjørn Borg, ------------------------------------ 1234Bjør Arne Pedersen, ------------------------- 3456Asfandyar Wali Khan Karzai bin Laden ---2356Without the lines off course :)Any ideas how I can acheive that when i have this code:My code is:Table tbl = new Table(); TableCell cell = new TableCell(); cell.Text = e.Item.DataItem.ToString(); cell.Text = ((DataRowView)e.Item.DataItem)[0].ToString(); TableCell cell2 = new TableCell(); string tlfnr = ((DataRowView)e.Item.DataItem)[1].ToString(); tlfnrtlfnr = tlfnr.Replace(" ", string.Empty).Trim(); cell2.Text = (tlfnr.Length > 4) ? tlfnr.Substring(tlfnr.Length - 4) : ""; TableRow row = new TableRow(); row.Cells.Add(cell); row.Cells.Add(cell2); tbl.Rows.Add(row); e.Item.Controls.Add(tbl) I tried by using css - classes on the cells, but without succsess.Any ideas? :-)Regards

Link to comment
Share on other sites

You would use a string pad function to pad the strings to a certain length. The character to pad the strings with is " ". Since that's actually 6 characters, you might have to pad using a space and then replace the spaces with  . You could also put the string in an element like a span or div and use CSS to specify the width of it.

Link to comment
Share on other sites

It looks like you're using .NET. Have you thought about a Repeater?

<asp:Repeater ID="PhoneRepeater" runat="server"><HeaderTemplate>	<table cellspacing="0" cellpadding="0" border="0">		<thead>			<tr>				<th>Name</th>				<th>Phone Number</th>			</tr>		</thead>		<tbody></HeaderTemplate><ItemTemplate>			<tr>				<td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>				<td><%# DataBinder.Eval(Container.DataItem, "PhoneNumber") %></td>			</tr></ItemTemplate><FooterTemplate>		</tbody>	</table></FooterTemplate></asp:Repeater>

And then, in the codebehind:

// If you have a DataTable ("myDataTable") which has Columns// named "Name" and "PhoneNumber", you can bind it directly to the RepeaterPhoneRepeater.DataSource = myDataTablePhoneRepeater.DataBind();

Link to comment
Share on other sites

It looks like you're using .NET. Have you thought about a Repeater?
<asp:Repeater ID="PhoneRepeater" runat="server"><HeaderTemplate>	<table cellspacing="0" cellpadding="0" border="0">		<thead>			<tr>				<th>Name</th>				<th>Phone Number</th>			</tr>		</thead>		<tbody></HeaderTemplate><ItemTemplate>			<tr>				<td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>				<td><%# DataBinder.Eval(Container.DataItem, "PhoneNumber") %></td>			</tr></ItemTemplate><FooterTemplate>		</tbody>	</table></FooterTemplate></asp:Repeater>

And then, in the codebehind:

// If you have a DataTable ("myDataTable") which has Columns// named "Name" and "PhoneNumber", you can bind it directly to the RepeaterPhoneRepeater.DataSource = myDataTablePhoneRepeater.DataBind();

Thanks alot for your input, mate. I have now managed to make it work. Yes, I am using .Net, but I have to make everything programatically, and can't use design view or asp code, because I am developing something called web parts for sharepoint.
Link to comment
Share on other sites

I have to make everything programatically....
Hmm, I haven't had to make a Repeater programmatically. If I were to do this all in C#, it'd look something like:
StringBuilder sb = new StringBuilder();sb.Append("<table cellspacing=\"0\" cellpadding=\"0" border=\"0\">");sb.Append("<thead><tr><th>Name</th><th>Phone Number</th></tr></thead>");sb.Append("<tbody>");foreach(DataRow row in myDataTable){	sb.Append("<tr><td>");	sb.Append(row["Name"].ToString());	sb.Append("</td><td>");	sb.Append(row["PhoneNumber"].ToString());	sb.Append("</td></tr>");}sb.Append("</tbody></table>");Literal literal = new Literal();literal.Text = sb.ToString();Page.Controls.Add(literal);

But, then again, you said you got it working. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...