Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int i = dataGridView1.Rows.Count;
           for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
           {
               if (dataGridView1.Rows[i].Cells[1].Value.ToString() == txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text)
               {
                   dataGridView1.Rows[i].Selected = true;
               }
               else
               {
                   MessageBox.Show("Customer with CNIC'" + txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text + "' does not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
           }
Posted

Just make this modification and your MessageBox will only appear once.
C#
int i = dataGridView1.Rows.Count;
bool itemFound = false;
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
   if (dataGridView1.Rows[i].Cells[1].Value.ToString() == txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text)
   {
      dataGridView1.Rows[i].Selected = true;
      itemFound = true;
      break;
   }   
}
if( !itemFound )
{
   MessageBox.Show("Customer with CNIC'" + txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text + "' does not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 
Share this answer
 
First thing remove the message box out of the for loop.

Next try to build a string (preferablly using stringbuilder) that contains all customer codes with error.
Finally display this in the message box outside the for loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900