Index datagridview with re-ordered columns problem

zaaephod

New member
Joined
Jan 15, 2010
Messages
3
Programming Experience
5-10
I have a fairly complicated app that uses a datagridview to display some database table data. I've set up routines to save the dgv layout such as column widths, column ordering, column visibility, etc.

VB.NET:
DG.Rows(0).Selected = True

What I've run into is that code like this naturally selects the *table* column zero, instead of what's visible to the user as column zero.

Column zero in my database is an auto incrementing primary key, so that column is hidden from the user. Additionally, the columns can be re-ordered by the user, further complicating the issue of determining what actually is the left most column in the grid that's visible to the user.

Is there a way to figure out what is the left most column in the datagridview visually, as seen by the user?

Hope this makes sense!
Thanks in advance.

Z
 
VB.NET:
DataGridView1.FirstDisplayedScrollingColumnIndex
If you mean left most visible column regardless of which column is first currently displayed by horizontal scrolling then:
VB.NET:
DataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Index
 
Thanks!

Thank you, that info led me in the right direction.

In the end, I was able to use a for next to loop through the columns and retrieve their displayindex, which turned out to be what I needed.

VB.NET:
Form1.DG.Columns(p).DisplayIndex
 
Last edited by a moderator:
Back
Top