Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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
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 4-Jan-13 23:19pm
Edited 6-Jan-13 3:39am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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]
  Permalink  
v2
Comments
zeshanazam at 5-Jan-13 4:28am
   
coding is behind the button, multiselect property is true.
OriginalGriff at 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 at 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 at 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:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Update your code like this
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);
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 7,273
1 OriginalGriff 5,587
2 Peter Leow 4,097
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2015
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