开发者

datagridview/math problem.... please help

开发者 https://www.devze.com 2023-04-03 12:48 出处:网络
private void Remove_Click(object sender, EventArgs e) { dgvSelected.Rows.Remove(dgvSelected.CurrentRow);
private void Remove_Click(object sender, EventArgs e)
    {
       dgvSelected.Rows.Remove(dgvSelected.CurrentRow);
       price = Convert.ToDecimal(Price.Text);
       price -= Convert.ToDecimal(dgvSelected.SelectedCells[3].Value.ToString开发者_JAVA百科());
       Price.Text = price.ToString();         
    }

In that code, the purpose is to remove a selected row which has item name and price in a datagridview named dgvSelected and after removing, the removed row's price is to be subtracted to the current total. the problem is that when it come to the last row and if it was removed, it then makes an error saying "Object reference not set to an instance of an object." in the "price -= Convert.ToDecimal(dgvSelected.SelectedCells[3].Value.ToString());" part.


In your code you are removing the row before reducing the price from the actual price, so there an exception of "Object reference not set to an instance of an object" try removing the row after deducting the prices.


Do the math before removing the row -- then it will still exist.

private void Remove_Click(object sender, EventArgs e)
    {
       price = Convert.ToDecimal(Price.Text);
       price -= Convert.ToDecimal(dgvSelected.SelectedCells[3].Value.ToString());
       Price.Text = price.ToString();         
       dgvSelected.Rows.Remove(dgvSelected.CurrentRow);
    }


Do the math operations before removing the CurrentRow. In the end when you removed all the rows CurrentRow will be null hence the exception.

0

精彩评论

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

关注公众号