all groups > asp.net datagrid control > august 2007 >
You're in the

asp.net datagrid control

group:

Inline Calculations for Databinding


Inline Calculations for Databinding Nathan Sokalski
8/20/2007 6:23:09 PM
asp.net datagrid control:
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.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

Re: Inline Calculations for Databinding Alvin Bruney [MVP]
8/20/2007 8:18:13 PM
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.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

[quoted text, click to view]

Re: Inline Calculations for Databinding Nathan Sokalski
8/20/2007 9:09:53 PM
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.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

[quoted text, click to view]

Re: Inline Calculations for Databinding
8/21/2007 12:00:00 AM
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.
Re: Inline Calculations for Databinding Nathan Sokalski
8/21/2007 8:49:47 PM
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.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

[quoted text, click to view]

Re: Inline Calculations for Databinding Alvin Bruney [MVP]
8/25/2007 9:11:58 PM
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();
}

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

[quoted text, click to view]

AddThis Social Bookmark Button