title: Calculating Sum on similar cell value in datagridview
body: <p>Here, in this <code>datagridview</code>, i want to caculate sum for similar departments.  </p>
  
  <p>That is if department is architecture on 1st row  and its <code>TMH</code> cell in below <code>datagridview</code> value is 36. </p>
  
  <p>And then on 2nd row my department is design and its TMH cell value is 45.</p>
  
  <p>And then again i have selected architecture department on 3rd row and its TMH value is now 45.</p>
  
  <p>Here is my code </p>
  
  <pre><code>           private void ProjectActivitiesGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
      {
          Decimal sum = 0;
  
          #region THM CALCULATION.
  
          int column = ProjectActivitiesGrid.CurrentCell.ColumnIndex;
          string headerText = ProjectActivitiesGrid.Columns[column].HeaderText;
  
          DataGridViewRow d = this.ProjectActivitiesGrid.Rows[e.RowIndex];
  
          String department = d.Cells[0].Value.ToString();
  
              String chk = m_Project.projdepthrs.Where(c =&gt; c.DEPARTMENT == department).Select(c =&gt; c.DEPARTMENT).FirstOrDefault();
  
              if (chk == null)
              {
                  MessageBox.Show("Please fill up department alloted hrs first");
                  d.Cells[1].ReadOnly = true;
                  d.Cells[2].ReadOnly = true;
                  d.Cells[3].ReadOnly = true;
                  d.Cells[4].ReadOnly = true;
                  d.Cells[5].ReadOnly = true;
                  d.Cells[6].ReadOnly = true;
                  d.Cells[7].ReadOnly = true;
  
              }
              else
              {
                  d.Cells[1].ReadOnly = false;
                  d.Cells[2].ReadOnly = false;
                  d.Cells[3].ReadOnly = false;
                  d.Cells[4].ReadOnly = false;
                  d.Cells[5].ReadOnly = false;
                  d.Cells[6].ReadOnly = false;
                  d.Cells[7].ReadOnly = false;
  
                  String tmh = m_Project.projdepthrs.Where(c =&gt; c.DEPARTMENT == department).Select(c =&gt; c.TMH).First();
                  LBLAllotedhrs.Text = tmh;
                  LBLLefthrs.Text = tmh;
              }
  
  
                 foreach (DataGridViewRow rw in ProjectActivitiesGrid.Rows)
                 {
                         if (department == d.Cells[0].Value.ToString())
                             sum = sum + Convert.ToInt32(d.Cells[5].Value);
                         else
                             sum = 0;
  
                 }
                  LBLLefthrs.Text = (Convert.ToInt32(LBLLefthrs.Text) - Convert.ToInt32(sum)).ToString(); 
  
          #endregion
  
  
      }
  </code></pre>
  
  <p>Then if how can i get my sum as 36+45 for 1st and 3rd row having similar department dropdown list.</p>
  
  <p>I want the logic for this situation.</p>