I have a strange outcome when I run this code – I have a datagridview with a basic stock list (name, onHand, Min) when the onHand drops below Min I want to have some kind of alert. The problem I am having is that I am giving the values to a messagebox so I can see what is happening and if I put the ‘onHand’ value in the top ‘if’ block it always comes out as zero but ‘min’ outputs the correct values. If I put the ‘Min’ value in the top ‘if’ block THAT comes out as zero and the ‘onHand’ shows the correct value. I have been trying toi fix it for a while now and can’t figure out what I am doing wrong. Currently I have ‘min’ in the top ‘if’ block and the messagebox always shows min as zero but gives ‘onHand’ the correct value.
The code:
private void btnLows_Click(object sender, EventArgs e)
{
int onHand = 0;
int min = 0;
int counter;
for (counter = 0; counter < (dataGridView1.Rows.Count);
counter++)
{
if (dataGridView1.Rows[counter].Cells["min"].Value
!= null)
{
if (dataGridView1.Rows[counter].Cells["min"].Value.ToString().Length != 0)
{
onHand = int.Parse(dataGridView1.Rows[counter].Cells["min"].Value.ToString());
if (dataGridView1.Rows[counter].Cells["onHand"].Value != null)
{
if (dataGridView1.Rows[counter].Cells["onHand"].Value.ToString().Length != 0)
{
onHand = int.Parse(dataGridView1.Rows[counter].Cells["onHand"].Value.ToString());
if (onHand < min)
{
MessageBox.Show(onHand.ToString(), min.ToString());
}
else
{
MessageBox.Show(onHand.ToString(), min.ToString());
}
}
}
}
}
This line (inside the first nested if-statement within your for-loop):
Should probably be:
Currently you never set
minto anything else but 0 when you define it.