Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
want to get value of cell 0 of selected rows in 2 text boxes..there are 2 selected rows but in both text boxes i am getting value of only one selected row. 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;
                  int j = dataGridView1.SelectedCells[0].RowIndex;
                  string str = dataGridView1.Rows[j].Cells[0].Value.ToString();
 
                  string[] gcnic;
                  gcnic = str.Split('-');
                  txtcnic1.Text = gcnic[0];
                  txtcnic2.Text = gcnic[1];
                  txtcnic3.Text = gcnic[2];
 
                  str = dataGridView1.Rows[j].Cells[0].Value.ToString();
 

                  gcnic = str.Split('-');
                  txtgcnic1.Text = gcnic[0];
                  txtgcnic2.Text = gcnic[1];
                  txtgcnic3.Text = gcnic[2];
              }
Posted 5-Jan-13 7:27am
Edited 5-Jan-13 7:40am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

want to get value of cell 0 of selected rows in 2 text boxes
Your code does almost opposite. You are comparing cell value with textbox text and doing some operation.
 
You need to do something like:
for(i=0;i<=dataGridView1.Rows.Count-1;i++)
{
  if (dataGridView1.Rows[i].Selected)
  {
     // selected row cell[0] concatenated value. 
     string str = dataGridView1.Rows[i].Cells[0].Value.ToString() + '-' + str;
  }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are setting all the textboxes to one row in the loop.
When the first selected row is looped over, all textboxes are set to values from this row.
When the next selected row is looped over, all textboxes are set to values from this second one.
 
As a result, you get all values from the second selected row.
  Permalink  
Comments
zeshanazam at 5-Jan-13 12:46pm
   
how to not allow the user to select any row of datagridview

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
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