There is a code that counts the first column of the matrix.

int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value); } label3.Text = sum.ToString(); 

how to modernize it so that it counts all the elements separately and brings them to the label, I don’t need the sum of all the columns at once, I need the comma separated the amount of each column in turn to be immediately output, with a single click on the button. What is there to put in a cycle to realize my idea?

    1 answer 1

    The easiest way is to make a loop in a loop.

     private void button1_Click(object sender, EventArgs e) { for (int j = 0; j < dataGridView1.Rows.Count; j++) { int sum = 0; //тут пробегаем не по строке, а по столбцу for (int i = 0; i < dataGridView1.ColumnCount; i++) { sum += Convert.ToInt32(dataGridView1[j,i].Value); } //заносим сумму в лист, и переходим к следующей итерации //не забываем перед этим обнулить счетчик listBox1.Items.Add(sum); } }