Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. long.Parse of datagrid cell value

long.Parse of datagrid cell value

Scheduled Pinned Locked Moved ASP.NET
helporaclequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Colin Mc
    wrote on last edited by
    #1

    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:

    M 1 Reply Last reply
    0
    • C Colin Mc

      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:

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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 a Literal control declared in the ItemTemplate of a TemplateColumn, 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 the e.Item.DataItem property. Also, you should remember to check the ItemType to determine what row this code applies to as this method is called for every row of a datagrid control.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups