C# 如何计算两个datagridview单元格,并在用户输入后立即在第三个单元格中显示结果?

C# 如何计算两个datagridview单元格,并在用户输入后立即在第三个单元格中显示结果?,c#,winforms,C#,Winforms,在c#winform for TextBox Text_change事件中,我们看到,只要我们键入,我们就可以计算两个文本框,并且总和显示在另一个文本框中。因此,我们可以对数据网格视图执行相同的textchange事件。我已经找到了计算两个单元格的代码,但是在我们按下tab按钮之后 private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >

在c#winform for TextBox Text_change事件中,我们看到,只要我们键入,我们就可以计算两个文本框,并且总和显示在另一个文本框中。因此,我们可以对数据网格视图执行相同的textchange事件。我已经找到了计算两个单元格的代码,但是在我们按下tab按钮之后

private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{ 
  if (e.RowIndex > -1)
   {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            string valueA = row.Cells[TRP2.Index].Value.ToString();
            string valueB = row.Cells[Quantity.Index].Value.ToString();
            decimal result;
            if (Decimal.TryParse(valueA, out result)
                    && Decimal.TryParse(valueB, out result))
            {
                decimal v1 = Convert.ToDecimal(valueA);
                decimal v2 = Convert.ToDecimal(valueB);
                row.Cells[Total.Index].Value = v1 * v2;
            }
  }
   dg1SalesCalculation();
}
先谢谢你

关于
Manoj

我很久以前就找到了这个答案。我正在发布这个代码。在此之前,应首先选择datagridview,在属性-->事件-->中双击CellValidated。这将生成事件。现在在代码中:-首先清除变量,然后是CellValidated代码。 在这里,我有两列Rate和Qty,第三列叫做Amount来存储答案。代码如下:-

string valueTRP;
string valueQTY;
private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex > -1)
    {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            if (row.Cells[Rate.Index].Value.ToString() != "")
            {
                valueTRP = row.Cells[Rate.Index].Value.ToString();
            }
            else
            {
                valueTRP = "0";
            }
            if (row.Cells[Qty.Index].Value.ToString() != "")
            {
                valueQTY = row.Cells[Qty.Index].Value.ToString();
            }
           if (Decimal.TryParse(valueTRP, out result) && Decimal.TryParse(valueQTY, out result))
            {
                decimal vTRP = Convert.ToDecimal(valueTRP);
                decimal vQTY = Convert.ToDecimal(valueQTY);
                decimal z = vTRP * vQTY;
                string x = z.ToString("F");
                row.Cells[Amount.Index].Value = x;
            }
    }
}

显示一些您尝试使用datagridview的代码。使用datagridview 1_CellValueChanged Events感谢美国Petrosov。但它不起作用。我应该更改代码吗?