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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with datagridview Frozen property 2

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
Hi, i'm having a strange problem here. I have a big datagridview with like 2000 rows. If I freeze the first rows, no problem, the row will be frozen and i'll be able to scroll through the other entries.

However, if I scroll down to the 100th entry for example, and freeze it, the scrollbar will disappear, and it will bring me to the beginning of the data grid..

Is it possible to fix this bug?

(On right click of a row)
_dgProducts.Rows[_dgProducts.CurrentRow.Index].Frozen = true;
 
It seems you can't freeze more rows than would be visible on the grid. I tried scrolling through the grid to fix this issue but it always scrolls to the top when you set the frozen rows. The one solution I found was to HIDE some of the frozen rows. The ones that wouldn't have been visible anyway.
Something like this:

Code:
int visibleFrozenRows = 10;
int hideTo = _dgProducts.CurrentRow.Index - visibleFrozenRows;
for (int x = 0;x <= hideTo;x++) {
_dgProducts.Rows[x].Visible = false;
}
_dgProducts.Rows[_dgProducts.CurrentRow.Index].Frozen = true;

that will only show visibleFrozenRows with the rest of the rows scrollable underneath. Make sure that visibleFrozenRows is set to less than the number of rows visible on your DataGridView. Not sure if that is the behavior you're going for but it works.




Travis Hawkins
 
I think that is what i'll have to do..

Is there a way to know the last row VISIBLE on the viewport?

This way I could calculate the difference between the last and the selected one, and using your code, freezing X rows before the selection, instead of having a specified number of rows to keep visible.
 
Yes, I know there are like 2446 rows total displayed in the datagrid.

There's 19 rows always visible in the view port.

Is it possible to get the index of the lastest VISIBLE row, and not the lastest row of the table?
 
Oooh, I misunderstood. You need to look at
_dgProducts.FirstDisplayedCell or _dgProducts.FirstDisplayedScrollingColumnIndex

you might need to use the second one if a freeze is already setup and the first if there is none.
Anyway, check them both you'll figure it out.

Travis Hawkins
jobs.bestcodingpractices.com
 
Oh, thanks a lot, that's perfect!

The first one is the good one :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top