Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to select gridview rows which contains text box values, there are two rows that contains text box value. but the code is selecting only one row..both should selected. help me with this. ThanX
C#
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);
          }
Posted
Updated 6-Jan-13 2:39am
v4

You need to make sure that the MultiSelect property of the DataGridView is set to "true" - it is by default, but if you have set it to "false" it will exhibit the behaviour you describe.

[edit]Typo: "MultiSecelt" for "MultiSelect" - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
zeshanazam 5-Jan-13 4:28am    
coding is behind the button, multiselect property is true.
OriginalGriff 5-Jan-13 4:31am    
Then you need to look at your data very carefully - make sure that at least two items are passing your test, because if MultiSelect is true then setting the Selected property does not remove the selection from any other rows.
zeshanazam 5-Jan-13 4:39am    
data from database is loaded in gridview when form load, i m just searching data by clicking search button and entering data in text boxes
OriginalGriff 5-Jan-13 4:49am    
Oops! Sorry, I missed a bit in your code...and so did you!
Take out the line:
break;
If you want to select more than one row! :laugh:
Update your code like this
C#
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; Commented this line
    }
 }

if (!itemFound)
{
   MessageBox.Show("Customer with CNIC'" + txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text + "' does not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 
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