The DataGridView can contain multiple SELECTED rows (if you set property MultiSelect to True) but should only contain a single CurrentCell - this is the cell that is currently active (being edited for example). Set the CurrentCell property to a non-hidden non-disabled, non-header cell and that will move the black arrow to the row that contains that cell. This row can be checked with the CurrentRow (read-only) property of the dgv.

For example in VB:

DataGridView1.CurrentCell = DataGridView1.Rows(1).Cells(0)

Hope this helps.

PS Best to put separate issues on separate threads - you can always reference one thread from another using a hyperlink.

// this is the row you want selected programaticallyint RowYouWantToSelect;

// get the rowcountint rowCount = dgv_FinancialList.RowCount;

// set a flag for existance of rowbool rowExists = false;

// loop through the rows in the data gridfor (int i = 0; i < rowCount; i++){ // check to see if this is the row you want to remember if (Convert.ToInt32(DataGridView.Rows.Cells["ID_Field"].Value) == RowYouWantToSelect) { // set CurrentCell equal to this row, you can choose any cell in the row DataGridView.CurrentCell = DataGridView.Rows.Cells[0];

// set flag for existance of row rowExists = true;

// break out, you don't need to loop anymore break; }}

// if row does not exist, set current cell to first row.if (!rowExists){ DataGridView.CurrentCell = DataGridView.Rows[0].Cells[0];}

Thanks for the post Hank. I just wanted to add one additional point -- when setting the CurrentCell property, it's important that the Cells[x] that is chosen is visible. If it is hidden, then an exception will be thrown.

Just stumbled across this thread, as it's exactly the problem I'm now trying to solve. All good, except for one point. My datagridview is large enough that the record I'm trying to refocus on is off the screen. i.e. the cell is not visible.

I have a slightly different question along these lines if someone can help me out.

I have a multi-select datagridview. I have added a contextmenu to my rows when adding them based on a column value. I only allow a row selection. When you right click on the row, the contextmenu pops up, but it does NOT select the row that was right clicked on.

I solved this problem by checking the mousedown event, checking to see if it was the right mouse button that was clicked and then selecting the row. The result is like a multi-select selection with the CTRL key pressed. If the SHIFT key is pressed it works the same way, which is not correct.

I started writing all kinds of code to make the right click to perform the multi-select in all cases like a left click before popping up my contextmenu. I'm having a hard time doing this and it seems the wrong approach.

I want the right click to behave like the left click (select row and perform correctly for multi-select) and then pop open the contextmenu.

I think this will help, use the context menu opening event. This way the position of the contect menu is at the mouseposition automatically. The cell iss selected to get the rowpointer moved. The the row gets selected.

privatevoid cmuGrid_Opening(object sender, CancelEventArgs e)

{

// get the mouse porsition and convert it to coordinate within the grid

I think this will help, use the context menu opening event. This way the position of the contect menu is at the mouseposition automatically. The cell iss selected to get the rowpointer moved. The the row gets selected.

privatevoid cmuGrid_Opening(object sender, CancelEventArgs e)

{

// get the mouse porsition and convert it to coordinate within the grid

hi, I have a similar problem,everything a set of value is insert to my table then display on DataGridviewwhen everytime the datagridview refresh, how do I make the datagridview select the last (latest) entry???

This information helped me get the programatically selected row displayed, but the scroll bar slider is not positioned correctly. I added a new row to the bottom of the grid programatically and the code sample code helped me get the new row displayed. However, the scroll bar slider is positioned at the top of the scroll bar instead of the bottom. As soon as the user takes some action that causes the grid to adjust (move to a different row, sort, etc.), the slider jumps to the proper position. Is there some way to programatically reset the slider?

I'd like to note something. I'm doing something similar to all of you, but using SINGLE SELECT rows in the DataGridView.

I'm developing a software like the "Microsoft Training Kits" but using files from braindumps dot com. I adapt them to fit in a xml file and an application will compare your answers with those from the xml file (application calculates your score, randomizes questions, limits your time, and things like that).

Initially, I've used a listbox to ennumerate questions. But I've decided to replace it by a DataGridView just because I wanted an icon next to the question list to see which answers were Ok, bad or empty. I wanted to do that way just because I needed to gain knowledge of using image columns and a few topics more.

You can navigate the question list with your mouse, but certain frantic people like me, wants to navigate with keyboard shortcuts. So I created two methods: one for going forward and another going backward. Of course, it just could be one.

I didn't realized why the rows weren't painted "blue" when changing selection... and no row were selected after all...

This is what happens: when you set the DataGridView to SINGLE SELECT, at all times there must be a single row selected. My methods first check selected row, select next or previous row, and then deselected the old row. This is wrong, because for a lilttle time, two rows where selected at once. DataGridView does not throw an exception, and no error were showing anywhere.

Conclusion: if you need to navigate between rows, first store the selected index, then use DataGridView.ClearSelection() and then select the row you need. That, of course, is what it works.

I hope this will help someone who had the same problem I had.Ing Rosell

Not sure if this is the correct place for this post or even if anyone is still monitoring the thread....

My DGV has blank cells which is fine they are not compulsary and i dont have a problem with them.I can't select a blank cell. In fact i can't even select the area around a populated cell. Anyone else seen this and have i just missed a simple property?