Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView Selected Row Indicator 1

Status
Not open for further replies.

KDavie

Programmer
Feb 10, 2004
441
US
This is driving me crazy... I've put way too much time into something that should be a relatively simple task...

I am programmatically changing the selected row on a datagridview. For the most part everything works as expected, however, the selected row indicator (the little arrow in the left margin) does not update with the selected row change. Consequently, the grid doesn't scroll to the selected row. This is really annoying behavior and I can't seem to see how to control it... Any suggestions?

 
I came up with a work around...

Code:
Dim cell As System.Windows.Forms.DataGridViewCell = dgMakes.Rows(node).Cells(0)
dgMakes.FirstDisplayedCell = cell

This will force the datagridview to scroll to the selected row, however the indicator still doesn't advance. In this particular case it's not a big deal, I just made the rowColumn invisible. However, if anyone knows how to advance that indicator I would really like to know how!! Thanks.

 
I too struggled with this for a while. I just came up with a solution.
Let's say my DataGridView is called "dataGridViewBillCodes" and I want the new selected row index to be x (assuming x is a valid index in the DataDridView) and I also want the row selection indicator to move to that same row.

The following code will do that and display correctly:

dataGridViewBillCodes.Rows[x].Selected = true;
dataGridViewBillCodes.FirstDisplayedScrollingRowIndex = x;
dataGridViewBillCodes.CurrentCell =
dataGridViewBillCodes.Rows[0].Cells[0];

Good luck!
 
Sorry, that last line should be ...Rows[x].Cells[0];:

dataGridViewBillCodes.CurrentCell =
dataGridViewBillCodes.Rows[x].Cells[0];

Mea culpa!
 
Nice... Thanks!!! I will give this a try.

-Kevin
 
faq796-6492 is worth reading

________________________________________________________
Zameer Abdulla
Help to find Missing people
Take the first step in faith. You don't have to see the whole staircase, just take the first step.
(Dr. Martin Luther King Jr.)
 
Don't know if you have solved your problem, but the last line in my code:

dataGridViewBillCodes.Rows[x].Selected = true;
dataGridViewBillCodes.FirstDisplayedScrollingRowIndex = x;
dataGridViewBillCodes.CurrentCell =
dataGridViewBillCodes.Rows[x].Cells[0];

is what seems to force the Row selection indicator to point to the Selected Row.

It seems that setting the Row Selected property to true and adjusting the FirstDisplayedScrollingRowIndex to point to that Row sould be enough, but it's not. It's not until you set the CurrentCell that the Row selection indicator actually updates to your Row.

Correct me if I am wrong!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top