Inline Calculations for Databinding

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
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.
 
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.
 
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.
 
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.
 
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.
 
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();
}
 
Back
Top