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. General Programming
  3. C#
  4. Totaling and averaging column in datagridview [modified]

Totaling and averaging column in datagridview [modified]

Scheduled Pinned Locked Moved C#
5 Posts 3 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.
  • M Offline
    M Offline
    Martin31088
    wrote on last edited by
    #1

    Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written: int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString(); It loops through once, and then fails at: Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martin

    modified on Wednesday, April 8, 2009 9:24 AM

    D M 2 Replies Last reply
    0
    • M Martin31088

      Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written: int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString(); It loops through once, and then fails at: Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martin

      modified on Wednesday, April 8, 2009 9:24 AM

      D Offline
      D Offline
      Dino Mulahusic
      wrote on last edited by
      #2

      its because you have only one row SELECTED and you are looping through all rows in datagridview try

      Average = datagridview.Rows[Counter].Cells[2].Value.ToString();

      to loop through all rows or

      while (Counter < datagridview.SelectedRows.Count)

      to loop only through selected rows

      M M 2 Replies Last reply
      0
      • M Martin31088

        Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written: int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString(); It loops through once, and then fails at: Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martin

        modified on Wednesday, April 8, 2009 9:24 AM

        M Offline
        M Offline
        MNantu
        wrote on last edited by
        #3

        It fails because ? What is the exception ? :)

        1 Reply Last reply
        0
        • D Dino Mulahusic

          its because you have only one row SELECTED and you are looping through all rows in datagridview try

          Average = datagridview.Rows[Counter].Cells[2].Value.ToString();

          to loop through all rows or

          while (Counter < datagridview.SelectedRows.Count)

          to loop only through selected rows

          M Offline
          M Offline
          MNantu
          wrote on last edited by
          #4

          Oops I missed that out :-O

          1 Reply Last reply
          0
          • D Dino Mulahusic

            its because you have only one row SELECTED and you are looping through all rows in datagridview try

            Average = datagridview.Rows[Counter].Cells[2].Value.ToString();

            to loop through all rows or

            while (Counter < datagridview.SelectedRows.Count)

            to loop only through selected rows

            M Offline
            M Offline
            Martin31088
            wrote on last edited by
            #5

            Thanks alot, I knew it would be something simple like that.

            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