Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to set focus on updated datagridview row after update in c# .net windows application?

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

Possibly this could help you:
C#
dataGridView1.ClearSelection();
int nRowIndex = dataGridView1.Rows.Count - 1;

dataGridView1.Rows[nRowIndex].Selected = true;
dataGridView1.Rows[nRowIndex].Cells[0].Selected = true;

Here i focus on first row of datagridview after update. But you need to focus on updated row,right? So keep track which row is updated and find out that row index and assign that value in nRowIndex ,rest of the work are done in above code.
 
Share this answer
 
v3
Comments
[no name] 23-Aug-13 2:53am    
I write this code my datagridview fully refereshed.
Member 13580360 28-Jan-18 11:53am    
Working Fine
after update , you clear the selection of datagridview like this

dataGridView1.ClearSelection();


and then select the row which you want to focus.
 
Share this answer
 
try this...

if you want to focus any cell of current row

dataGridView1.CurrentRow.Cells[2].Selected = true;



and if you want to select the whole row then simple write this code

dataGridView1.CurrentRow.Selected = true;
 
Share this answer
 
Comments
[no name] 23-Aug-13 1:47am    
my problem it did not solve, my datagridview refresh and focus on first row after update
Member 11783104 23-Jun-15 7:47am    
but sir it is not work.i want to set the focus on the row which contains studid field as 12.
just you target on grid cell which one contain primary field data

and set those value on any control on the time of editing
like i have a primary field those name is "className" and it's possition on grid is cell[2] for each row and when i click on edit button this primary field value set on txtclassname text control.


This code is used to select updated Row
C#
foreach (DataGridViewRow row in dgvClassData.Rows)
  {
     if (row.Cells[2].Value.ToString().Equals(txtClassName.Text))
       {
          dgvClassData.Rows[row.Index].Selected = true;
       }
  }


Thanks & Regard
Sham :)
 
Share this answer
 
Comments
cempreng 25-Apr-14 5:33am    
many thanks... it most brilliant way..
Best Regards.
While updating row take its index in a temporary variable and then in binding method check whether that temporary variable has a value or not. if the variable has value then make that row index as selected true.
 
Share this answer
 
 
Share this answer
 
Comments
Dave Kreskowiak 7-Feb-16 20:16pm    
Guy, this is a 3 year old question. I don't think he's looking for the answer any more and the answer you posted doesn't really add anything to the discussion.

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