Here's something simple ... use an int.TryParse() instead of Convert.ToInt32(). What that will do is default the result to 0 if it's not a valid integer (null in your case). So, something like this:
int QtyOnHand, QtyOnHold, QtySold, QtyAvailable;
foreach (DataGridViewRow row in dgv.Rows)
{
if (string.IsNullOrEmpty(row.Cells["QtyOnHand"].Value.ToString()))
{
int.TryParse(row.Cells["QtyOnHand"].Value.ToString(), out QtyOnHand);
int.TryParse(row.Cells["QtyOnHold"].Value.ToString(), out QtyOnHold);
int.TryParse(row.Cells["QtySold"].Value.ToString(), out QtySold);
int.TryParse(row.Cells["QtyAvailable"].Value.ToString(), out QtyAvailable);
row.Cells["QtyAvailable"].Value = QtyOnHand - QtyOnHold - QtySold - QtyAvailable;
}
}
~~Bonnie DeWitt [C# MVP]
No comments:
Post a Comment