Reputation: 367
In Asp.net I have a listview that has a column called "Amount". Negative numbers are displayed as (111.111) and porsitive #s are sdisplayed as 111.11. How can I right-align the numbers on decimal? If there wasn't any parenthesis it would be easy. However, this is a requirement.
Thanks
Upvotes: 1
Views: 495
Reputation: 22578
Color the parenthesis the same as the background when the number is non-negative. Right align.
<asp:ListView ID="MainListView" runat="server">
<LayoutTemplate>
<table style="text-align:right;">
<tr id="ItemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<span runat="server" style='<%# GetStyle() %>'>(</span>
<%# Eval("Item") %>
<span runat="server" style='<%# GetStyle() %>'>)</span>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
protected void Page_Load(object sender, EventArgs e)
{
var values = new[] {
new { Item = 123.12},
new { Item = -133.34}
};
MainListView.DataSource = values;
MainListView.DataBind();
}
protected string GetStyle()
{
if ((double)Eval("Item") < 0)
return string.Empty;
else
return "color: white;";
}
Upvotes: 1
Reputation: 4024
That is going to be tricky.
There are two ways I can think of off hand to solve this problem.
Split the value up into two columns. Right align the first column which contains anything before the decimal. Left align the second column which contains the decimal and any trailing digits. The downside to this is that it is in two different cells, which means you can't simply double click the content to copy & paste it, etc.
Use Graphics.MeasureString
to calculate exactly how many pixels of padding you need to put on the column.
Upvotes: 0