long.Parse of datagrid cell value
-
i have a datagrid populated from an Oracle datbase and the last cell for each row is a number. i want to loop through each item and add up all the numbers i tried; long total = total + long.pasre(datagrid.items[loop].cells[6].Text) but i get the error "input string was not in correct format" can anybody help? the row in the oracle table is a number in case that helps? Thanks Colin :confused:
-
i have a datagrid populated from an Oracle datbase and the last cell for each row is a number. i want to loop through each item and add up all the numbers i tried; long total = total + long.pasre(datagrid.items[loop].cells[6].Text) but i get the error "input string was not in correct format" can anybody help? the row in the oracle table is a number in case that helps? Thanks Colin :confused:
Hi Colin, The error happens due to the fact that the value passed to the parse method is not a valid value (perhaps, it might be empty), you can easily see this thing when running your application in debug mode. In this case, there are a number of ways to do what you want: + You can use a
BoundColumn
for this number data, then use the sample code in your post to calculate the total value. + You can use aLiteral
control declared in theItemTemplate
of aTemplateColumn
, the sample code to calculate the total value is something like this:long total = 0;
for(int loop=0; loop<DataGrid1.Items.Count; loop++)
{
//'Literal1' is the id of the Literal control.
Literal lit = DataGrid1.Items[loop].FindControl("Literal1") as Literal;
total += long.Parse(lit.Text);
}+ You create an event handler for the
ItemDataBound
event of the DataGrid column. In the event handler, you can get the number value with the help of thee.Item.DataItem
property. Also, you should remember to check theItemType
to determine what row this code applies to as this method is called for every row of a datagrid control.