We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Grid scroll position not updated correctly when size changes

Hi, I have a glitch to report concerning the grid's scroll position handling. This is the scenario; I have a grid on a form that I scroll down on, say, 10-20 lines down. Now I resize the form resulting in the grid also being resized (dock full). In my Size Changed Event Handler I adjust the three columns' widths to fully use the width of the grid. Up to now it works just fine, but: The top visible row is the same as it was when the grid was smaller, i.e. even though the grid now has space for much more rows it only displays as may as there were before resizing the grid. In addition to that the vertical scroll bar gets hidden (which is correct though). However, the only way to get the 10-20 other lines back into the visible area is to either use the cursor keys or the mouse wheel. Is there a way I could hint the grid to recalculate the number of visible rows and adjust the top visible row accordingly? Regards Kai Iske DWS Investments

6 Replies

AD Administrator Syncfusion Team April 24, 2003 02:53 PM UTC

A question. If the top row is on line 15 when you start sizing, if empty clientarea starts to appear under the grid, do you expect the toprow to decrease to try to fill this empty clientarea? If so, in your sizedchanged event, you could sum the heights of the visible rows (form TopRowIndex down to RowCount since the last row is visible) comparing this sum to the clientheight, and decrease TopRowIndex until you fill the clientheight. Or, is it some other problem you are seeing?


KI Kai Iske April 25, 2003 02:55 AM UTC

Clay, thanks for your suggestion. Please find my notes below: > A question. If the top row is on line 15 when you start sizing, if empty clientarea starts to appear under the grid, do you expect the toprow to decrease to try to fill this empty clientarea? True, this is exactly what I am trying to accomplish. Actually I was expecting the Grid to do this for me as it already determines that there are sufficient visible rows to display the full grid, resulting in the vertical scrollbar to disappear/be disabled. However, it does not adjust the TopRowIndex to actually show all rows. Maybe there is a good point in the way it is implemented at the momend, but to be honest, I don't see it right now :) > If so, in your sizedchanged event, you could sum the heights of the visible rows (form TopRowIndex down to RowCount since the last row is visible) comparing this sum to the clientheight, and decrease TopRowIndex until you fill the clientheight. Now I have it implemented using the following code. As I'm using a custom derived grid throughout my project it would be easy to add this code to the base class in order to have all grids behave the same way, but, again, shouldn't this be part of the root implementation? GridViewLayout layout = grid.ViewLayout; int visible = grid.RowCount - grid.TopRowIndex; if(visible < layout.VisibleRows) { int topIndex = Math.Min(0, grid.RowCount - layout.VisibleRows); grid.TopRowIndex = topIndex; }


KI Kai Iske April 25, 2003 03:02 AM UTC

Whoops, should have been Math.Max(...) rather than Math.Min(...)


KI Kai Iske April 25, 2003 03:29 AM UTC

Note: This is not spam :) Of course I have missed a couple of issues. The code I have posted earlier does not take into consideration: 1. Row heights 2. Hidden rows So my TopRowIndex calculation is more or less accurate resulting in good results with standard height/not hidden rows. Any help/hint regarding these issues would be highly appreciated. Thanks


AD Administrator Syncfusion Team April 25, 2003 07:28 AM UTC

Kai, I think I am losing my mind. Yesterday when I looked at this I did a little sample, and immediately saw what you are talking about. Now this morning, I bring up that sample, and do not see the problem. As I size the grid taller, the top row reduces to try to fill the clientheight. I have swap out versions a couple of times since I originally looked at the sample. This morning I have tried it with 1.5.2.0 and tbe beta code for 1.6, and do not see the problem. What version are you using so I can try it (in case something has been corrected that fixes this problem). As far as handling hidden rows or variable row heights, you can probaly use code such as this (could not check it out as I no longer see the behavior). The first loop is your code setting the colwidths. The part adjusts the toprowindex if needed:
private void gridControl1_SizeChanged(object sender, System.EventArgs e)
{
	int width = this.gridControl1.ClientRectangle.Width - this.gridControl1.ColWidths[0];
	for(int i = 1; i < 4; ++i)
		this.gridControl1.ColWidths[i] = width / 3;

	if(this.gridControl1.TopRowIndex > this.gridControl1.Rows.FrozenCount + 1
		&& this.gridControl1.ViewLayout.LastVisibleRow == this.gridControl1.RowCount)
	{
		int topRow = this.gridControl1.TopRowIndex;
		int targetHeight = this.gridControl1.ClientSize.Height 
			- this.gridControl1.RowHeights.GetTotal(0,this.gridControl1.Rows.FrozenCount);
 		int currentHeight = this.gridControl1.RowHeights.GetTotal(topRow, this.gridControl1.RowCount);
		while (topRow > this.gridControl1.Rows.FrozenCount + 1 &&
			targetHeight > currentHeight + this.gridControl1.RowHeights[topRow-1])
		{
			topRow--;
			currentHeight += this.gridControl1.RowHeights[topRow];
		}
		this.gridControl1.TopRowIndex = topRow;
	}
}


KI Kai Iske April 29, 2003 09:42 AM UTC

Clay, thanks for all your help. I had the time to look through my code and all the grid's properties. I have found the cause of my problem: As I did not want to have any horizontal scrolling, I have set the HScrollBehavior to Disabled and thought that it would be a good idea to set VScrollBehavior to Enabled. I have now set VScrollBehavior back to default (DetectIfShared) and now it does what it is supposed to be doing: It adjusts the top row index. Regards Kai Iske > Kai, > > I think I am losing my mind. > > Yesterday when I looked at this I did a little sample, and immediately saw what you are talking about. Now this morning, I bring up that sample, and do not see the problem. As I size the grid taller, the top row reduces to try to fill the clientheight. I have swap out versions a couple of times since I originally looked at the sample. This morning I have tried it with 1.5.2.0 and tbe beta code for 1.6, and do not see the problem. What version are you using so I can try it (in case something has been corrected that fixes this problem). > > As far as handling hidden rows or variable row heights, you can probaly use code such as this (could not check it out as I no longer see the behavior). The first loop is your code setting the colwidths. The part adjusts the toprowindex if needed: >
> private void gridControl1_SizeChanged(object sender, System.EventArgs e)
> {
> 	int width = this.gridControl1.ClientRectangle.Width - this.gridControl1.ColWidths[0];
> 	for(int i = 1; i < 4; ++i)
> 		this.gridControl1.ColWidths[i] = width / 3;
> 
> 	if(this.gridControl1.TopRowIndex > this.gridControl1.Rows.FrozenCount + 1
> 		&& this.gridControl1.ViewLayout.LastVisibleRow == this.gridControl1.RowCount)
> 	{
> 		int topRow = this.gridControl1.TopRowIndex;
> 		int targetHeight = this.gridControl1.ClientSize.Height 
> 			- this.gridControl1.RowHeights.GetTotal(0,this.gridControl1.Rows.FrozenCount);
>  		int currentHeight = this.gridControl1.RowHeights.GetTotal(topRow, this.gridControl1.RowCount);
> 		while (topRow > this.gridControl1.Rows.FrozenCount + 1 &&
> 			targetHeight > currentHeight + this.gridControl1.RowHeights[topRow-1])
> 		{
> 			topRow--;
> 			currentHeight += this.gridControl1.RowHeights[topRow];
> 		}
> 		this.gridControl1.TopRowIndex = topRow;
> 	}
> }
> 
>

Loader.
Live Chat Icon For mobile
Up arrow icon