Help with Repeater control and advanced data manipulation

  • Thread starter Thread starter Mike Cain
  • Start date Start date
M

Mike Cain

Hi,

The Repeater control seems like exactly what I want to output rows of data
from my database. However I need to do some manipulation to the data prior
to it being output and I'm not understanding how to go about this properly.

For instance, the db column "myVal" is defined as a double in the db. In my
table I want to display the value of that number by 1000. I used the code
below and it does the trick. However, for some rows myVal may be null
(DBNull) so in that case I get an exception thrown. I can't exclude rows
where myVal is null because its just one field I need to display for the
row. If it is null, I'd like to output "N/A" instead of the value/1000.
How can this be done?
<itemtemplate>

<tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"myVal"))/1000 %></td></tr>

<tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"myVal2"))/1000 %></td></tr>

</itemtemplate>

Along these same lines - is there a way to create some program logic and
display the results of that logic in the Repeater template? For instance
even if there is a simple solution to the above, I have other cases where I
need some fancy if/then/else logic to really determine the value I want to
display in the table for a given data element.

Am I better off reading the rows out of the table into a temp array and
putting the values into the array as I want them to display, and then
binding the array to the Repeater instead of trying to work with the dataset
directly in the Repeater? If so, are there any good examples you can point
me to for this?

Thanks in advane!!
 
Mike said:
Hi,

The Repeater control seems like exactly what I want to output rows of
data from my database. However I need to do some manipulation to the
data prior to it being output and I'm not understanding how to go
about this properly.
For instance, the db column "myVal" is defined as a double in the db.
In my table I want to display the value of that number by 1000. I
used the code below and it does the trick. However, for some rows
myVal may be null (DBNull) so in that case I get an exception thrown.
I can't exclude rows where myVal is null because its just one field I
need to display for the row. If it is null, I'd like to output "N/A"
instead of the value/1000. How can this be done?
<itemtemplate>

<tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"myVal"))/1000 %></td></tr>

<tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"myVal2"))/1000 %></td></tr>

</itemtemplate>

Write a helper function and call that function from your template:
(I'm using VB.NET)
Function Helper(obj As Object) As String
If(IsDbNull(obj)) Then
Return "N/A"
Else If ...
...
End If
End Function

<tr><td><%# Helper(DataBinder.Eval(Container.DataItem,
"myVal")) %></td></tr>
 
ItemDataBound event is the answer to all your questions. Research it first
and then come back and ask more if you need.

Good luck,
 
Back
Top