Hi Boaz,
Thanks for your post!
There is no direct method of DataGrid class to scroll the grid
programmatically, however, I have 2 indirect way to achieve this purpose:
1. Manually set a DataGrid.CurrentCell which is in a specific position:
//before the following operation, you will probably have to save the
Current DataGrid1.CurrentCell information in order to retrive them lately.
DataGrid1.CurrentCell = Nothing
DataGrid3.CurrentCell = New DataGridCell(targetXcell, targetYcell)
//The DataGrid1 will scroll to the position where the cell(targetXcell,
targetYcell) in the Client View.
2. Send Windows message(WM_HSCROLL) to the DataGrid object via
SendMessage() Win32 API
public enum MsgType
{
SB_LINELEFT = 0,
SB_LINERIGHT = 1,
SB_PAGELEFT = 2, //same value as SB_PAGEUP
SB_PAGERIGHT = 3, //same value as SB_PAGEDOWN
SB_THUMBPOSITION = 4,
SB_THUMBTRACK = 5,
SB_LEFT = 6,
SB_RIGHT = 7,
SB_ENDSCROLL = 8,
WM_HSCROLL = 0x00000114
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, String lpszClass, String lpszWindow);
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, MsgType uMsg, int
wParam, int lParam);
...
//the horizonal scroll bar's classID
IntPtr hHScroll = FindWindowEx(dataGrid1.Handle, (IntPtr)0,
"WindowsForms10.SCROLLBAR.app3", null);
IntPtr hVScroll = FindWindowEx(dataGrid1.Handle, hHScroll,
"WindowsForms10.SCROLLBAR.app3", null);
SendMessage(dataGrid1.Handle, MsgType.WM_HSCROLL,
(int)MsgType.SB_PAGERIGHT,(int)hHScroll);
SendMessage(dataGrid1.Handle, MsgType.WM_VSCROLL,
(int)MsgType.SB_PAGERIGHT,(int)hVScroll);
Hope it helps!
Gary Chang
Microsoft Online Partner Support
Get Secure! ?
www.microsoft.com/security
This posting is provided "AS IS" with no warranties,and confers no rights.
--------------------
| From: "Boaz Ben-Porat" <
[email protected]>
| Subject: Simulate horizontal scroll
| Date: Thu, 9 Oct 2003 16:41:53 +0200
| Lines: 15
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 195.215.64.74
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:190248
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi all
|
| Is there a way to simulate a click on a scrollbar (horizontal) of a
FataGrid
| ?
|
| I need to programmatically scroll the grid to the right/left upon some
| condition, when the total width of the grid columns is greater then the
| grid`s rectangle.
|
| TIA
| Boaz Ben-Porat
| DataPharm a/s
| Denmark
|
|
|