Click here to Skip to main content
12,076,206 members (46,038 online)
Rate this:
 
Please Sign up or sign in to vote.
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 4-Jan-13 7:19am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Just make this modification and your MessageBox will only appear once.
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);
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100