Click here to Skip to main content
Sign Up to vote bad
good
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 - 22:19
Edited 6 Jan '13 - 2:39


2 solutions

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  
Comments
zeshanazam - 5 Jan '13 - 4:28
coding is behind the button, multiselect property is true.
OriginalGriff - 5 Jan '13 - 4:31
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:39
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:49
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
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
Your Filters
Interested
Ignored
     
0 Mahesh Bailwal 353
1 Sergey Alexandrovich Kryukov 339
2 Maciej Los 290
3 CPallini 245
4 Rohan Leuva 175
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,194
2 CPallini 3,923
3 Rohan Leuva 3,176
4 Maciej Los 2,633


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid