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. How do I add Selected Cells

How do I add Selected Cells

Scheduled Pinned Locked Moved C#
question
4 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.
  • K Offline
    K Offline
    kornstyle
    wrote on last edited by
    #1

    I am using a datagridview control. I want to select multiple cells that contain numbers and add them. What would be the best way to do this? Thanks.

    N V 2 Replies Last reply
    0
    • K kornstyle

      I am using a datagridview control. I want to select multiple cells that contain numbers and add them. What would be the best way to do this? Thanks.

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello

      int Sum = (int)MyDataTable.Rows[RowIndex].ItemArray[Index].Value + (int)MyDataTable.Rows[SecondRow].ItemArray[SecondIndex].Value;

      And so on... Regards:rose:

      1 Reply Last reply
      0
      • K kornstyle

        I am using a datagridview control. I want to select multiple cells that contain numbers and add them. What would be the best way to do this? Thanks.

        V Offline
        V Offline
        Vitaliy Tsvayer
        wrote on last edited by
        #3

        If I understand you right, you use DataGridView control with MultipleSelect set to true and when you select some cells you want to add numbers within them. To do that you can handle SelectionChanged event like below:

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        {
        //NOTE: we should check whether value in cell is a numeric value
        //add values of cell
        }
        }

        BUT, be carefull to check whether selected cells has an integer value!

        Vitaliy Tsvayer Tikle

        K 1 Reply Last reply
        0
        • V Vitaliy Tsvayer

          If I understand you right, you use DataGridView control with MultipleSelect set to true and when you select some cells you want to add numbers within them. To do that you can handle SelectionChanged event like below:

          private void dataGridView1_SelectionChanged(object sender, EventArgs e)
          {
          foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
          {
          //NOTE: we should check whether value in cell is a numeric value
          //add values of cell
          }
          }

          BUT, be carefull to check whether selected cells has an integer value!

          Vitaliy Tsvayer Tikle

          K Offline
          K Offline
          kornstyle
          wrote on last edited by
          #4

          Thanks for your help. That worked great.

          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