Datagridview列总计

时间:2017-03-06 11:31:03

标签: c# mysql datagridview subtotal

我尝试在datagridview中计算名为QTY的列的总和。 但我不会。我的代码如下。请问你能帮帮我吗? 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();

    }

2 个答案:

答案 0 :(得分:1)

试试这个:

foo

答案 1 :(得分:0)

试试这个

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();
}