Inline Calculations for Databinding

N

Nathan Sokalski

I have two fields in the Datatable that I am using as my DataSource that I
need to multiply together before displaying them. Here is the databinding
expressions from my ASPX file to display the fields separately:

'<%# Databinder.Eval(Container.DataItem,"price","{0:C}") %>'
'<%# Databinder.Eval(Container.DataItem,"quantity","{0:D}") %>'

These are obviously both numeric values, but because my DataTable is not
created from a database, I cannot add an extra field by modifying my SQL
statement. Is there a way to somehow include an expression in the
databinding expression that multiplies the two values together? The only
alternative that I can think of is to use the ItemDataBound event and
manually assign the product of the two values to the desired control, but
because this requires using CType and FindControl, it is rather inefficient.
Any other ideas? Thanks.
 
A

Alvin Bruney [MVP]

Why post this to so many groups, it's not necessary at all. In any case, you
will need CType, you won't necessarily need FindControl. If you know the
column, you can use the appropriate index to the cell.
 
N

Nathan Sokalski

How can I do it without FindControl? The place I would be using FindControl
would be when assigning the value to the control, like in the following:

CType(e.Item.FindControl("lblTotal"), Label).Text = "$" &
CStr(CSng(e.Item.DataItem("price")) * CInt(e.Item.DataItem("quantity")))

I realize that I could use the Controls property of e.Item, but that can
sometimes be hard to determine, and if I were to add or remove controls that
are above this control, the index would change. Were you talking about a
different technique? If so, could you give me an example? Thanks.
 
C

ca8msm

How do you create your DataTable? It would seem easier to me to just
create another column at the same time and add both the values
together.
 
N

Nathan Sokalski

I create my DataTable manually by adding DataColumns to a DataTable and then
adding data using the DataTable's Rows.Add method. The reason I would prefer
not to create an extra column is because I have to store the DataTable in
ViewState, which, even though I will rarely have more than about 10 or so
records in the DataTable, it can increase the size of the client's download,
as well as require me to update more fields. If I could do the
multiplication inline, it would allow me to have only one extra area in my
code that is different, and would not increase the size of the client's
download.
 
A

Alvin Bruney [MVP]

I would prefer the datatable approach because the datatable contains a
compute method and a filter function that makes this sort of thing easy.
however, if you insist you would need to do something like this to avoid the
find control.

//make sure you have data
if(e.item.cells > 2)
{
//get input one and two
string input = e.item.cells[1].Text
int i = int.tryparse(input);

string input2 = e.item.cells[2].Text
int ii = int.tryparse(input2);

//perform the math and stick it back in a column
e.item.cells[2].Text = (i * ii).ToString();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top