C# 文本框中的datagridview列和

C# 文本框中的datagridview列和,c#,C#,我想要datagridview文本框中的列和 private void AddButton_Click(object sender, EventArgs e) { dataGridView1.Rows.Add(SNoTextBox.Text, PriceTextBox.Text, QtyTextBox.Text); foreach(DataGridViewRow row in dataGridView1.Rows) { row.Cells[dataGri

我想要
datagridview
文本框中的列和

private void AddButton_Click(object sender, EventArgs e)
{
    dataGridView1.Rows.Add(SNoTextBox.Text, PriceTextBox.Text, QtyTextBox.Text);

    foreach(DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["Qty"].Index].Value));
    }
}
我正在使用上面的代码在
datagridview
中插入数据,我想要
GrandTotalTextBox
中的
AMOUNT
列的总和

试试这个:

private void AddButton_Click(object sender, EventArgs e)
{
    decimal amount =0;
    dataGridView1.Rows.Add(SNoTextBox.Text, PriceTextBox.Text, QtyTextBox.Text);

     foreach(DataGridViewRow row in dataGridView1.Rows)
     {
           row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["Qty"].Index].Value));

           amount += Convert.ToDecimal(row.Cells[dataGridView1.Columns["Amount"].Index].Value);
      }
    GrandTotalTextBox.Text = amount.ToString();
}

还有一个
Linq
解决方案:

private void AddButton_Click(object sender, EventArgs e)
{
    dataGridView1.Rows.Add(SNoTextBox.Text, PriceTextBox.Text, QtyTextBox.Text);

    foreach(DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["Qty"].Index].Value));
    }
}

List<decimal> list = dataGridView1.Rows
         .OfType<DataGridViewRow>()
         .Select(r => Convert.ToDecimal(r.Cells["Amount"].Value.ToString()))
         .ToList();

GrandTotalTextBox.Text = list.Sum().ToString();
private void AddButton\u单击(对象发送者,事件参数e)
{
dataGridView1.Rows.Add(SNoTextBox.Text、PriceTextBox.Text、QtyTextBox.Text);
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
行.Cells[dataGridView1.Columns[“Amount”].Index].Value=(Convert.ToDouble(行.Cells[dataGridView1.Columns[“Price”].Index].Value)*Convert.ToDouble(行.Cells[dataGridView1.Columns[“Qty”].Index].Value));
}
}
List List=dataGridView1.Rows
第()类
.Select(r=>Convert.ToDecimal(r.Cells[“Amount”].Value.ToString())
.ToList();
GrandTotalTextBox.Text=list.Sum().ToString();
在他的友好帮助下