Completely disabling Scrolling in a DataGridView
-
In my current (Windows Forms Application) project, I have a form which includes a number of DataGridViews, which are used to display tabular data for the user to look at and edit. The data sets are of known, fixed, size, and the DGVs are designed to allow the data sets to fit neatly within them. The ability to scroll the view (in any direction) is neither wanted nor needed. I have disabled the scroll bars and set AllowUserToAddRows to false, but navigating down with the arrow keys still causes a blank row to appear at the bottom and the top row of data to disappear; arrowing all the way up reverses this. One odd aspect of this is that the scrolling up happens when you arrow down from the last but one to the last real row. If you use the mouse to select a cell in the last row and then try to arrow down, nothing happens. There is what appears to be a fair amount of discussion of this problem in various places, but the solution generally given is to disable the scroll bars and set AllowUserToAddRows to false, which doesn't seem to work for me. I have also tried capturing the Scroll Event and repositioning the Selected Cell to the top of the DGV within it, which sort of works, but this has other unwanted side-effects. Has anyone else run into this problem and/or come up with a good solution?
-
In my current (Windows Forms Application) project, I have a form which includes a number of DataGridViews, which are used to display tabular data for the user to look at and edit. The data sets are of known, fixed, size, and the DGVs are designed to allow the data sets to fit neatly within them. The ability to scroll the view (in any direction) is neither wanted nor needed. I have disabled the scroll bars and set AllowUserToAddRows to false, but navigating down with the arrow keys still causes a blank row to appear at the bottom and the top row of data to disappear; arrowing all the way up reverses this. One odd aspect of this is that the scrolling up happens when you arrow down from the last but one to the last real row. If you use the mouse to select a cell in the last row and then try to arrow down, nothing happens. There is what appears to be a fair amount of discussion of this problem in various places, but the solution generally given is to disable the scroll bars and set AllowUserToAddRows to false, which doesn't seem to work for me. I have also tried capturing the Scroll Event and repositioning the Selected Cell to the top of the DGV within it, which sort of works, but this has other unwanted side-effects. Has anyone else run into this problem and/or come up with a good solution?
After spending a lot of time researching this, I was able to come up with a solution which works, but there should be an easier way! There are a lot of 'gotchas', apparently 'baked in' to the design of the control. Not only does the view scroll up (by default) when you arrow down into a cell in the last row, but it also scrolls up if you arrow left or right while in the last row. Implementing my solution involves a number of steps: 1) Subclass the DataGridView control so that you can (optionally) override its ProcessCmdKey method to disable special handling of the arrow keys (you don't actually need to mess with the up arrow key, but I found it easier to disable all of them. 2) Substitute references to your new MyDataGridView control for those to the original version in the containing Form's Designer.vb file - for me, using VS 2013, two references needed to be changed for each affected control. Then, either in the designer or in initialization code, set the option to disable the arrow keys for the modified controls. 3) Write code for the KeyUp and KeyDown Events for each affected control - the KeyDown code does apparently need to be there, but it only has to set the Handled event argument to True; the KeyUp code has to do the work of changing the current cell appropriately for the arrow key pressed. For any arrow keystroke that results in a move to a cell in the bottom row (including a lateral one), the KeyUp code also has to set the control's FirstDisplayedScrollingRowIndex Property to 0 after implementing the move. This is what scrolls the view back down again to abolish the empty row, but it does cause a visible 'glitch'. 4) If you don't want the view to appear to 'glitch' when the user is moving to or on the bottom row, it is best to surround the working KeyUp code with calls to disable and re-enable screen painting while it is (in effect) moving the focus about.