Windows Develop Bookmark and Share   
 index > Windows Forms General > run time set focus to gridview
 

run time set focus to gridview

hi i am able to add a new row to my databounded datagrdiview. this is my code
 DataRow dr = dt.NewRow();
              
                dt.Rows.Add(dr);
                gvEditSale.DataSource = dt;

                int idx = gvEditSale.Rows.GetLastRow(DataGridViewElementStates.Visible);
                gvEditSale.Rows[idx].Selected = true;
The new row is selected and highlighted. However, i failed to set the edit focus to the row which is selected = true. The small arrow on the rowheader is still remained at the original row eventhough the focus has moved to the new row.
Please advice


alain
alainkkk  Thursday, October 08, 2009 9:37 AM
The arrow points to the position within the list or dataset that your grid is hooked up to.  It looks like you're setting the data source of the grid to be the new row you create, which is cutting off the link to your original data source.  You shouldn't have to change the data source in any case.  Try removing the line:

gvEditSale.DataSouce = dt;
Daniel Smith  Thursday, October 08, 2009 10:12 AM
Hi alain,

If you want to make last row as edit row, just select it is not enough. You need to make one of the cells of the last row as the CurrentCell.

For example:
int idx = this.dataGridView1.Rows.GetLastRow(DataGridViewElementStates.Visible);
dataGridView1.CurrentCell = dataGridView1[0, idx];

Now you can see the small arrow on the row header has been moved to the last row.

Please feel free to tell me if you have any question.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn[At]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  4 hours 14 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• picturebox image
• AS soon as user hits Enter tab while typing password submit button get focus
• How to bind control state to another control state
• SuspendLayout and Layout Event on a UserControl
• Late binding/Invoke Script
• Sending an Event
• How can I set ListBox.HorizontalExtent to the good value.
• how can i save the text format to the database?
• Text Editor
• Form Basics