开发者

Auto Compute of Inputs in DataGridView - C#

I just wa开发者_Python百科nt to ask. I have a datagridview of Products with columns ProdName, Quantity, SellingPrice, and Total. The Quantity and SellingPrice both has a ReadOnly property set to false. So basically, the user can change values in it during run-time. What I want to know is how can I insert a value in the Total column which is actually the product of the Quantity and the SellingPrice?

Any inputs are welcome. Thanks :)


You could use the CellValidated event:

private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    // TODO: Add proper error handling by checking for null values, 
    // using decimal.TryParse, ...
    var row = dataGridView1.Rows[e.RowIndex];
    int quantity = int.Parse(row.Cells[1].Value.ToString());
    decimal sellingPrice = decimal.Parse(row.Cells[2].Value.ToString());
    row.Cells[3].Value = quantity * sellingPrice;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜