Saturday, June 28, 2014

Need to replace null value by zero to calculate

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]


http://ift.tt/1bPbIRj



No comments:

Post a Comment