Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data grid row formating 1

Status
Not open for further replies.

Zarcom

Programmer
May 7, 2002
1,275
CA
I have a datagrid that displays some numbers representing dollar figures. Currently if there is no money in that field aka 0 then $0.00 is displayed. I would like to for display purposes display nothing rather thant the $0.00. Can I do this? I am sure I can I am just not sure where to start. That'l do donkey, that'l do
[bravo] Mark
 
Can I see how you're putting the number into the field? i.e. the template column definition? There are many different ways, so I'd rather show you using your existing stuff.

:)
paul
penny1.gif
penny1.gif
 
umm I just bound the grid to a dataset and specified which columns that I wanted to display.

This is one of the columns that I wanted the display nothing for

<asp:BoundColumn DataField=&quot;Tax1&quot; SortExpression=&quot;Tax1&quot; HeaderText=&quot;Tax1&quot; DataFormatString=&quot;{0:C}&quot;></asp:BoundColumn>

Under the grid databinding event I was trying something like so. Still working on this.

Dim row As DataGridItem
Dim cell As TableCell

For Each row In DataGrid1.Items
For Each cell In row.Cells
If cell.Text = &quot;$0.00&quot; Then
cell.Text = &quot;&quot;
End If
Next
Next


Am I on the right track or completely off here?
Thanks Paul. That'l do donkey, that'l do
[bravo] Mark
 
Go the template column route. For example:

<asp:BoundColumn DataField=&quot;Tax1&quot; SortExpression=&quot;Tax1&quot; HeaderText=&quot;Tax1&quot; DataFormatString=&quot;{0:C}&quot;></asp:BoundColumn>

could be changed to:

<asp:templateColumn HeaderText=&quot;Tax1&quot; SortExpression=&quot;Tax1&quot;>
<itemTemplate>
<%# FormatMyMoney(Container.DataItem(&quot;Tax1&quot;)) %>
</itemTemplate>
</asp:templateColumn>

then, you just declare your cleaning function in the code-behind:

protected function formatMyMoney(obj as object) as string
dim output as string
if isDBNull(obj) then
output = string.empty
else
if not obj.toString = string.empty then
output = formatCurrency(obj)
else
output = string.empty
end if
end if
return output
end function

this assumes that there are either definitely numbers, empty strings, or null values in the datasource. if there are more possibilities, then you need to just take care of those in the function.

I really like this method. You can use the same function to clean out as many currency fields as you wish.

Hope that helps! :)
paul
penny1.gif
penny1.gif
 
Looks excellent. A question 4 U though.

What exactly is the difference between a Bound Column and a Template Column That'l do donkey, that'l do
[bravo] Mark
 
Well, a bound column is simply less flexible than a template column. It just spits out the data for non-edit state, and gives you the standard text box for edit state.

For example, in a recent datagrid I put out, I had five columns of bit data that I needed to represent in a single column of the datagrid.

In a non-edit state, the column was to have simple text in it (that had to be created from the five columns, a comma separated string of descriptions).

In an edit state, the column had to contain a table with five columns, a checkbox in each, and each representing the current 1/0 state of the data fields.

Here is a rough approximation of what it looked like, just to fill you with ideas. ;-)

<asp:templateColumn>
<itemTemplate>
<%# returnServiceText(Container.DataItem(&quot;f1&quot;),Container.DataItem(&quot;f2&quot;).... (&quot;f5&quot;))%>
</itemTemplate>
<editItemTemplate>
<table cellpadding=1 cellspacing=0 border=0 width=100%>
<tr>
<td>Field1</td>
<td>Field2</td>
<td>Field3</td>
<td>Field4</td>
<td>Field5</td>
</tr><tr>
<td align=center>
<asp:checkbox id=ckb1 checked=<%# returnChecked(Container.DataItem(&quot;f1&quot;)) %> />
</td>
<td align=center>
<asp:checkbox id=ckb1 checked=<%# returnChecked(Container.DataItem(&quot;f2&quot;)) %> />
</td>
<td align=center>
<asp:checkbox id=ckb1 checked=<%# returnChecked(Container.DataItem(&quot;f3&quot;)) %> />
</td>
<td align=center>
<asp:checkbox id=ckb1 checked=<%# returnChecked(Container.DataItem(&quot;f4&quot;)) %> />
</td>
<td align=center>
<asp:checkbox id=ckb1 checked=<%# returnChecked(Container.DataItem(&quot;f5&quot;)) %> />
</td>
</tr>
</table>
</editItemTemplate>
</asp:templateColumn>

so that then all I had were two functions in the codebehind... the first one accepted five arguments, evaluated the 1/0 state of each, and returned a string variable describing that (i.e. &quot;desc1,desc3&quot; if only f1 and f3 were true).

Then, I had another very simple function that returned the state of each individual field to either check or uncheck the editable checkboxes when the row was in edit state.

So... that was a little long-winded, but with template columns, you can do just about anything you can imagine w/in the context of a datagrid. It makes each column completely customizeable for those applications where you need more than just a standard type of display.

I have thought of putting together a comprehensive FAQ on the datagrid, but to be honest, there's just too much information, and once you start, you would have to make a discertation out of it to be complete and make it really helpful.

:)
paul
penny1.gif
penny1.gif
 
OIC
The light has dawned! And it is time for a nap damn I am tired.

Did you mean this perhaps Paul? Instead of descertation
dis·ser·ta·tion Pronunciation Key (dsr-tshn)
n.
A lengthy, formal treatise, especially one written by a candidate for the doctoral degree at a university; a thesis.

kool I learned a new word today. That'l do donkey, that'l do
[bravo] Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top