I’m using a TemplateField to add an extra “calculated” column to a gridview that is bound to a table in a database. I’m getting this weird error and I have no clue how to begin to debug it. I might be doing something wrong/non-doable with the <%# syntax and if so please let me know what. I don’t fully understand it.
Code:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Math.Round(decimal.Parse((((int.Parse(Eval("Num1").ToString())) * 36) / (235 * int.Parse(Eval("Num2").ToString()))).ToString()), 0); %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Error:
CS1525: Invalid expression term ‘,’
There is a comma…but its for the second argument of Math.Round. Or am I doing something that is not do-able in this way? Also, the Math.Round line doesn’t indicate any errors in the code-behind (my parenthesis should be good).
It looked alright for me. However, it does seem like a fairly confusing line of text. If Num1 and Num2 are columns in your database, then you can just add a property on the class that represents the table.
So, next to where Num1 and Num2 are defined, define another property–not representing a column in the database, but a computation of values in the database.
This removes the computation from one specific page, so if you have to use it again somewhere, you can just reference the property.
Alternatively, you could reference a method on your code behind…
I like the property idea better, though, since it is reusable.