Resolved DataGridView - Prevent scrolling beyond the defined rows

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
130
Programming Experience
10+
I have a DataGridView that has two rows defined and is sized so that only the two rows and no background (grey area) is displayed. ScrollBars are set to "None". When I click on each cell with the mouse, everything works as expected. If I TAB or use the arrow keys or select a cell on the second row programmatically, the control scrolls and show Row two and the background. How can I stop this from happening?

DGVSelect1.png


DGVSelect2.png
 
Solution
It happens if the control size is too small in height to display the rows, so you can increase height until it fits exactly the number of rows you need displayed. There can be more than the visible rows, but if for example the control can show 1.99 rows then it will show 0.99 empty space after last row when you activate that row, that will not happen if it can show 2 full rows. You can try in designer to add height until it fits, or try something like this code:
C#:
grid.Height = 2 + grid.ColumnHeadersHeight + grid.Rows[0].Height * 2;
First of all, I think you are using the wrong control if you wish to display static data. Prefer to use the TableLayoutPanel instead of DataGridView. The DataGridView is mean to give the user an Excel or Access data sheet like experience.

Next, you should really be doing things in WPF, but based on your other threads, it seems like you already have a large sunken cost into WinForms. WPF gives you a lot more options for styling and controlling control behaviors.

With the WinForms DataGridView, I think there are ways to tell it it has a fixed number of rows and to disable scrolling, but I don't recall anymore. It's been years since I've had to use WinForms because I've treated it as EOL.
 
This is no secret that I hate the DGV control and I've never liked using it, so my advisable contributions here are limited. @jmcilhinney is the DGV guru for that control. Perhaps if he has spare time, he can give you some worth while advice to follow when he gets online. But you will need to wait, since he is in a different timezone to all of us.

I also second the advice to use WPF over winforms. Most devs who dive into application development lean towards the simplistic drag and drop form builders over the likes of WPF for simplicity, only to later regret not picking a more robust and modern platform for their application. If it's not to late, consider porting your app to WPF. You will also find the use of binding and the additional customisable options for controls much easier to work with within xaml.

And while I'm not sure if this is helpful to you regarding limiting the rows, you can do that as seen here : Adding a fixed number of rows in datagridview in c# in the .cs design form whether or not that will help with your problem, I am unsure, but you could try it.
 
It happens if the control size is too small in height to display the rows, so you can increase height until it fits exactly the number of rows you need displayed. There can be more than the visible rows, but if for example the control can show 1.99 rows then it will show 0.99 empty space after last row when you activate that row, that will not happen if it can show 2 full rows. You can try in designer to add height until it fits, or try something like this code:
C#:
grid.Height = 2 + grid.ColumnHeadersHeight + grid.Rows[0].Height * 2;
 
Solution
It happens if the control size is too small in height to display the rows, so you can increase height until it fits exactly the number of rows you need displayed. There can be more than the visible rows, but if for example the control can show 1.99 rows then it will show 0.99 empty space after last row when you activate that row, that will not happen if it can show 2 full rows. You can try in designer to add height until it fits, or try something like this code:
C#:
grid.Height = 2 + grid.ColumnHeadersHeight + grid.Rows[0].Height * 2;
JohnH,
Awesome. I thought I had the height set right. I used your code fragment and it worked like a charm!
 
Great, and if you're binding data or adding rows at a later time, and want to size the grid before the rows are added, you can use grid.RowTemplate.Height instead of grid.Rows[0].Height.
 
Back
Top Bottom