如何将两列的数据乘以datagridview中的第三列

问题描述:

我想将两列的数据相乘并在第三列中显示。

I want to multiply the data of two columns and show it in the third column.

例如:

1st Column: Quantity
2nd Column: Rate
3rd Column: Price

我想在用户输入数量和费率数据时相乘,例如 Quantity = 2 rate =我想在价格栏中自动显示50 100 。

I want to multiply as user enters the data for quantity and rate like Quantity=2, rate=50automatically in price column I want 100 to be appear.

同时我想要用户输入数量和费率数据(例如 Price = 100 rate = 50 )时自动进行数量划分希望出现 2

Simultaneously I want to division as user enters the data for quantity and rate like Price=100, rate=50automatically in Quantity column I want 2 to be appear.

当用户输入数量和费率的数据时,例如
我想要在 50 $ c中自动在费率列中 Price = 100 Quantity = 2 $ c>出现。

And when a user enters the data for quantity and rate like Price=100, Quantity=2automatically in Rate column I want 50 to be appear.

这三个将在同一datagridview中发生。用户只需输入这三个字段中的任何两个字段,第三个字段就会自动出现。

This three will happen in a same datagridview. User only enter any two field within this three and third will come automatically.

使用C#,VS2008,SQL 2008

Using C#, VS2008, SQL 2008

private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    int quantity,rate; 
    for (int i = 0; i < dataGridView2.Rows.Count; i++) 
    { 
        if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) 
        { 
             int price = quantity * rate; dataGridView2.Rows[i].Cells[3].Value = price.ToString(); 
        } 
    } 
}

我写这段代码。显示错误。对象引用未设置为

I write this code. There shows a error. Object reference not set to an instance of an object on

if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) 

此行。

我想做这个。用户可以在这3个字段中填写任意2个字段。第三个字段将自动填写。

I want to do like this. User can fill any 2 field among this 3. And third will take automatically.

处理gridview的 CellEndEdit 事件。然后在这种情况下,将值计算并分配给第三列

handle the CellEndEdit event of your gridview. And in that event calculate and assign the value to the third column

类似

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int quantity,rate;
        if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value.ToString(), out quantity) && int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["rate"].Value.ToString(), out rate))
        {
            int price = quantity * rate;
            dataGridView1.Rows[e.RowIndex].Cells["price"].Value = price.ToString();
         }
    }