Datagridview中整个列的总量传递到c#中的文本框

时间:2014-01-31 18:25:13

标签: c# visual-studio-2012

如何将datagridview中的总总金额提供给同一表单中的文本框?

// this part is calculating the total amount of a row
 private void SalesDataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (SalesDataGrid != null)
        {
            for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
            {
                SalesDataGrid.Rows[x].Cells[3].Value = Convert.ToInt32(SalesDataGrid.Rows[x].Cells[1].Value) *
                    Convert.ToDouble(SalesDataGrid.Rows[x].Cells[2].Value);

             //and i've tried this one below if it will work but it didn't do anything

            }
            if (SalesDataGrid != null)
            {
                for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
                {
                    txtsalestotal.Text = SalesDataGrid.Rows[x].Cells[3].Value.ToString() ;
                }

            }

1 个答案:

答案 0 :(得分:0)

试试这个:

double totalAmount=0;
for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
{
totalAmount += Convert.ToDouble(SalesDataGrid.Rows[x].Cells[3].Value.ToString());
}

txtsalestotal.Text=totalAmount.ToString();