Click here to Skip to main content
14,693,744 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I will type 5 in textBox1 and after click the button, 5 number row should be select in dataGridView1 .


What I have tried:

I will type 5 in textBox1 and after click the button, 5 number row should be select in dataGridView1
Posted
Updated 3-Sep-20 7:37am
Comments
Gerry Schmitz 3-Sep-20 10:10am
   
Then when you see "5" in the TextBox's TextChanged event, set the SelectedItem in the data grid.
Afzaal Ahmad Zeeshan 3-Sep-20 11:02am
   
You wrote it in English, now write this exactly the way it is in C#. :-)

//Got the Solution
private void button7_Click(object sender, EventArgs e)
     {
      int czc = 0;
        if (int.TryParse(textBox1.Text, out czc))
          {
          dataGridView1.CurrentCell = dataGridView1.Rows.OfType<DataGridViewRow>().Last().Cells.OfType<DataGridViewCell>().First();
           dataGridView1.Rows[czc].Selected = true;
          }
     }
   
Use int.TryParse to convert the textbox value to an integer - reporting any problems to the user - then use the converted value to set the row you want:
myDataGridView.Rows[index].Selected = true;
Do note that unless you clear all the selected flags from the other rows, this will add the row to the selected collection.
   
Comments
Afzaal Ahmad Zeeshan 3-Sep-20 11:02am
   
5ed.
Istehak Ahmad 3-Sep-20 11:15am
   
Thanks for the quick Replay, Can you share the complete code please?
OriginalGriff 3-Sep-20 12:00pm
   
You are kidding ... you can't use int.TryParse on your own?

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