Why one column?

Hi All

I am writing an ASP.NET program that adds TemplateFields to add columns to a GridView programmatically. Within one of these fields, I want to add a label that contains text that consists of two numbers in a string, the first is an integer and the second a percentage. I want these numbers to line up but of course the number of digits vary: 100%, 10%, 1% and so on. So I want to add varying number of spaces between the two numbers. I have tried PadLeft and PadRight but always the spaces are collapsed to one. So I have even tried clumsy code like this in desperation:

If p = 0 Then 'if percent is zero print a blank (not 0%)
l.Text = ""
ElseIf p > 0 And p < 0.1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & Space(4) & String.Format("{0[:#]#0%}", p)
ElseIf p >= 0.1 And p < 1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & Space(3) & String.Format("{0[:#]#0%}", p)
ElseIf p = 1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & Space(2) & String.Format("{0[:#]#0%}", p)
End If

I think that if I was writing html something like <span spaces="Preserve"> might do the trick. But how do I specify that in a TemplateField that is adding columns to a GridView programmatically?

There must be an easier way!

Flummoxed

Geoffrey

[2005 byte] By [GeoffreyMH] at [2008-1-7]
# 1

Hi,

Try using vbTab in your statements.

E.G.

Code Snippet

TextBox1.Text = number1.ToString & vbTab & number2.ToString

What you could do, in case you have numbers like;

12.3456 and

14.98765432

is use Math.Round( number1, 4 ) ' to round to 4 digits ( or whatever you like ) before your output command like the one above.

Regards,

S_DS

# 2

Hi S_DS

Many, many thanks for getting back to me on this and for your suggestions. Unforunately, they don’t seem to work.

Perhaps it would help if I explained what I was trying to do a little more fully.

I need to add columns to this GridView programmatically because I want to keep the column headings brief (to keep the table compact) but provide a Tooltip on the headings with a full description (which I can’t do with AutogenerateColumns true). Hence I am into the whole business of adding TemplateFields using a Public Class that implements Itemplate. I am using MSDN’s "How To: Create ASP.NET Web Server Control Templates Dynamically" as a guide (although this refers to a DataGrid) and also Mike Gunderloy’s elegant code in "Dynamic Template Columns in the ASP.NET 2.0 GridView Control" (although I have had to translate this from C#) as another model. And all this works fine.

But I want to give the user the option of displaying integers, percent or both in each column (nothing after the decimal point). Which also works fine, except that I can’t add them to the same label (actually a hyperlink) because I can’t format the leading spaces before the percent. Very frustrating!

I suppose I could create a tiny table with just two cells in the DataLabel_DataBinding event handler – but I don’t know how to do this. When I muddle html into this code it doesn’t work:

Dim l As HyperLink = CType(sender, HyperLink)

' get the containing row

Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)

' get the data and bind it to the hyperlink

Dim RawData As String

RawData = DataBinder.Eval(row.DataItem, columnName)

l.Text = ‘and so on


What can you recommend?

Kind regards

Geoffrey


GeoffreyMH at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Geoffrey MH wrote:

But I want to give the user the option of displaying integers, percent or both in each column (nothing after the decimal point). Which also works fine, except that I can’t add them to the same label (actually a hyperlink) because I can’t format the leading spaces before the percent. Very frustrating!

What can you recommend?

Kind regards

Geoffrey


Hi,

In HTML you can use one or more &nbsp followed by a semicolon in a string for a space character.

See >>

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_table_nbsp

and try this HTML in the "Try It editor" >>

Code Snippet

<html>
<body>

<table border="1">
<tr>
<td>Some &nbsp; text</td>
<td>Some text</td>
</tr>
<tr>
<td></td>
<td>Some text</td>
</tr>
</table>


As you can see, one of the cells has no border. That is because it is empty. Try to insert a space in the cell. Still it has no border.


The trick is to insert a no-breaking space in the cell.


No-breaking space is a character entity. If you don't know what a character entity is, read the chapter about it.


The no-breaking space entity starts with an ampersand ("&"),
then the letters "nbsp", and ends with a semicolon (";")

</body>
</html>

I know you may know that already? If not click on the "Edit the text and click me" button on that page towards the top left corner.

http://www.w3schools.com/ is a great site and it includes a section on ASP >>

http://www.w3schools.com/asp/default.asp

I hope it is of some help to you?

Regards,

S_DS

# 4
Hi S_DS

That's the problem...

label.text = "a" + &nbsp; + &nbsp; + &nbsp; + &nbsp; + "b"

comes out as "a b" not "a b",

Geoffrey

GeoffreyMH at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

Geoffrey MH wrote:
Hi S_DS

That's the problem...

label.text = "a" + &nbsp; + &nbsp; + &nbsp; + &nbsp; + "b"

comes out as "a b" not "a b",

Geoffrey

Hi Geoffrey,

Take a look at letter spacing using in-line styles or CSS ( cascading style sheet ) positioning.>>

http://w3schools.com/css/tryit.asp?filename=trycss_letter-spacing

Regards,

S_DS

# 6
Hi S_DS

Thanks for the tip.

The problem is that I am doing this in the context of a dynamically created GridView. Here is a bit more of the surrounding code. I hope this helps explain the situation

' event handler to perform the data binding
' called when data is bound to the GridView
Private Sub DataLabel_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
' get the control that raised this event
Dim l As HyperLink = CType(sender, HyperLink)
' get the containing row
Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
' get the data and bind it to the label
Dim RawData As String
RawData = DataBinder.Eval(row.DataItem, columnName)
Dim p As Single
If cumulativeSum > 0 Then
p = RawData / cumulativeSum
Else
p = 0
End If
Select Case displaySelected
Case 0 'display Numbers only
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData))
Case 1 'display Percent only
If p = 0 Then 'if percent is zero print a blank (not 0%)
l.Text = ""
Else
l.Text = String.Format("{0[:#]#0%}", p)
End If
Case 2 'display Both, adding spaces to keep digits aligned
If p = 0 Then 'if percent is zero print a blank (not 0%)
l.Text = ""
ElseIf p > 0 And p < 0.1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & vbTab & vbTab & vbTab & vbTab & String.Format("{0[:#]#0%}", p)
ElseIf p >= 0.1 And p < 1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & vbTab & vbTab & vbTab & String.Format("{0[:#]#0%}", p)
ElseIf p = 1 Then
l.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & vbTab & vbTab & vbTab & String.Format("{0[:#]#0%}", p)
End If
End Select
l.ToolTip = toolTipText
l.CssClass = "Normal"
End Sub

What do you think?

Regards

Geoffrey

PS sorry about the smileys

GeoffreyMH at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Hi S_DS

I have given up. Eiher my question is too dumb or too hard for this forum. I guess it is tied up somewhere with significant and insignificant spaces. I am now showing my numbers and percent like this...

label.Text = String.Format("{0[:#],###}", Integer.Parse(RawData)) & "(" & String.Format("{0[:#]#0%}", p) & ")"

Not pretty - the columns still don't line up - but, hell, what can you do?

Regards

Geoffrey

GeoffreyMH at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
Is there a reason why you don't just make two columns instead of one column with a label in it that has two values? If you had two columns, you could specify right-justify for both and they would line up as long as your formatting was consistent between rows.
Richard_Wolf at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
Hi Richard

That's a good question. Basically it's because I want the same heading over both columns. Like all things this code started easy enough. I wanted a table of numbers, but then I wanted Tooltips on the headings, then I wanted to generate the numbers from a pivot table, then I wanted to give the viewer three options: just to view the numbers, to see them as percent or to see both. And I wanted each GridView to look the same.

Perhaps there is a way to add a number and a calculated percent within a GridView cell programmatically, using DetailsView perhaps, but I don't see how and I thought it should be easier than this. Is there a way to add html in a label's string so that it renders two numbers correctly aligned - that's the question.

All suggestions welcomed

Geoffrey

GeoffreyMH at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

Hi Geoffrey,

This is sort of a shot in the dark... have you tried making sure your grid output font is a monospace font like Courier rather than a font that is not monospace (like Arial, Times New Roman, etc...)? That in conjunction with &nbsp; should allow you to line things up.

The down-side of course is that Courier is a rather ugly looking font.

Hope this helps.

DanRhea at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...