1

I tried to calculate the total of the column named QTY in the datagridview. But I coul not.My code is below. Could you help me , please? enter image description here

public int RowCount { get; set; }

    [BrowsableAttribute(false)]
    private void dataGridView1_SelectionChanged(object sender, EventArgs e)

    {

    private void UpdateLabelText()
    {
        int QtyTotal = 0;

        int counter;

        // Iterate through all the rows and sum up the appropriate columns.
        for (counter = 0; counter < (dataGridView1.Rows.Count);
            counter++)
        {
            if (dataGridView1.Rows[counter].Cells["Qty"].Value
                != null)
            {
                if (dataGridView1.Rows[counter].
                    Cells["Qty"].Value.ToString().Length != 0)
                {
                    QtyTotal += int.Parse(dataGridView1.Rows[counter].
                        Cells["Qty"].Value.ToString());
                }
            }
        }

        // Set the labels to reflect the current state of the DataGridView.
        label17.Text = "Qty Total: " + QtyTotal.ToString();

    }
Pikoh
  • 7,582
  • 28
  • 53
Bahadir
  • 11
  • 4

2 Answers2

1

Try this:

int sum = 0;
int col_index=YOUR_COLUMN_INDEX;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[col_index].Value);
}
label17.Text= sum.ToString();
imsome1
  • 1,182
  • 4
  • 22
  • 37
0

Try this

foreach (DataGridViewRow item in dataGridView2.Rows)
{
  int n = item.Index;
  text_tot.Text = (int.Parse(label17.Text) + int.Parse(dataGridView1.Rows[n].Cells[(index)].Value.ToString())).ToString();
}
imsome1
  • 1,182
  • 4
  • 22
  • 37
Rami Far
  • 406
  • 1
  • 10
  • 29