Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET: Data Grid View Jumps to the Top of the Page When it Gains Focus

Posted on 2010-09-04
15
Medium Priority
?
1,637 Views
Last Modified: 2012-05-10
Hi,

I have a Windows Form, containing a Panel with scroll bars. Within the Panel is a few text boxes at the top of the page and a Data Grid View with a lot of data in it. The Data Grid View starts about half way down the Panel and goes off the bottom, so you have to use the Panel scroll bars to view the bottom of the data.

This is the way it is supposed to look and work.

Unfortunately if the user goes onto the page and clicks a row close to the top, the Panel automatically scrolls so that the Data Grid View is at the top of the screen. It also selects a number of rows instead of just the clicked one.

Does anyone know of a solution to this, other than changing the structure of the form?

If you need any further information then please let me know.
0
Comment
Question by:philkeene
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33604648
Hi
check initially the anchor of the datagridview.also set multiselect to false.check these and let me know if you have a problem
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33605421
I don't think you need a panel on the form, because you can set datagridview to have its own scroll bars, both vertical and horizontal. Since the datagridview takes up the lower part of the form, you can set its anchor to "Top, Bottom, Left, Right" so that when the form re-sizes, the datagridview changes the size accordingly.
 
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33605434
Tab Index is a problem.

Please check available controls on your form, DataGrid view's tab index should be not smaller nor largest from all available controls those can take focus, reminder, label control cannot have focus, textbox, buttons can have focus.
0
Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

 
LVL 4

Author Comment

by:philkeene
ID: 33610302
Hi Guys,
I'm not sure how well i described my problem, but I have found the solution here.
http://www.daniweb.com/forums/showthread.php?t=6213&highlight=scroll+bar+problems+in+vb.net 
(Towards the bottom of the first page.)
Basically it says to create a new control inherited from the Panel, and to change the scrolltocontrol function to the following...
 
protected override Point ScrollToControl(System.Windows.Forms.Control activeControl) {
return this.AutoScrollPosition;
}
 
I haven't tried it yet but it looks like it should work fine.
Thanks for your help.
Kind regards,
Phil
0
 
LVL 4

Author Comment

by:philkeene
ID: 33610923
I was wrong it hasn't worked at all. I am using the following code for the inherited class...

Public Class NonAutoscrollingPanel
    Inherits System.Windows.Forms.Panel

        Protected Shadows Function ScrollToControl(ByVal activeControl As System.Windows.Forms.Control) As System.Drawing.Point
        MsgBox("Test")
        Return AutoScrollPosition
    End Function

End Class
 
When clicking on the data grid view it doesn't even trigger the message box.
Let me know if you have any further suggestions or suggested fixes for this code.
Many thanks.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33612338
Please post a screen shot of your form.
0
 
LVL 4

Author Comment

by:philkeene
ID: 33619471
Due to the sensitivity of the project I am unable to post the form, but instead I have recreated a very much simplified version of the form that still has all the components listed above and exhibits the same behaviour.
I have attached 2 screenshots to this post. The first is the form when it first loads up. The second is when the top row of the data grid view is clicked.
Let me know if you need any further information.
 

screenshot1.png
screenshot2.png
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 33619710
The screen shots confirmed my earlier comments. Like I said in my first post (the second in this thread), you do NOT need the panel at all, because the datagridview has its own scrollbars. It's a property called ScrollBars. You can set it at design time. When you have more data than the displaying area, the scrollbars will appear automatically.

0
 
LVL 4

Author Comment

by:philkeene
ID: 33621794
Yeah I thought that may be the case.
I was hoping for another solution, but I don't think there is one. There are a few reasons why I wanted to keep the panel, but in this case I think I'm going to have to submit and do it that way.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33621829
Well, if you still want to keep the panel, you can. Just set its scrollbars to None and make the datagridview control not going beyond the panel.
0
 
LVL 4

Author Comment

by:philkeene
ID: 33621847
Won't that do the same jumping behaviour, if it goes off the bottom of the screen?
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33622003
No it will not, because all the controls are on the screen. You scroll ONLY in the datagridview control. See the sample screenshot below:

Screenshot.PNG
0
 
LVL 4

Author Comment

by:philkeene
ID: 33622062
Ah right yes. That's fine then. I'll take that approach.
Many thanks for your help,
Phil
0
 
LVL 4

Author Comment

by:philkeene
ID: 33624781
Hi Everyone,
New update. The fix is possible after all, the link mentioned above did work, however I was not familiar enough with inheritance in vb.net to do it correctly. I replaced 'shadows' with 'overrides' and it all started working.
 

Public Class NonAutoscrollingPanel
    Inherits System.Windows.Forms.Panel


    Protected Overrides Function ScrollToControl(ByVal activeControl As System.Windows.Forms.Control) As System.Drawing.Point
        Return AutoScrollOffset
    End Function
End Class
 
 Thanks for your help.
Phil
0
 
LVL 4

Author Comment

by:philkeene
ID: 33626085
Slight correction...

Public Class NonAutoscrollingPanel
    Inherits System.Windows.Forms.Panel

    Protected Overrides Function ScrollToControl(ByVal activeControl As System.Windows.Forms.Control) As System.Drawing.Point
        Return AutoScrollPosition
    End Function

End Class
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question