Synchronously scroll two DataGrid

Source: Internet
Author: User
The first thought of this is to rewrite the Scroll method, but think about the workload is a little large, so I want to do something at the Form level, look at the list of members of the DataGrid, you can see the following two protective methods:
GridHScrolled Listens for the horizontal scrollbar's scroll event.
GridVScrolled Listens for the vertical scrollbar's scroll event.
It must be. Now let's start our own DataGrid. First, you need to create a solution. There are two projects, one is the Windows control library project and the WinForm project. The former is the DataGrid Control we write, and the latter is the control test project. A default class is available when you create a Windows user space. You have deleted or modified its name to crlDataGrid (you can call it by yourself ). We modify its inheritance relationship to let it inherit from the DataGrid. As follows:
Public class CrlDataGrid: System. Windows. Forms. DataGrid.
In this way, we can use our own DataGrid to publish the two methods mentioned above. As follows:
Public void crlGridVScrolled (object sender, ScrollEventArgs e ){

This. GridVScrolled (sender, e );

}

Public void crlGridHScrolled (object sender, ScrollEventArgs e ){

This. GridHScrolled (sender, e );

}
At this point, our control is complete. In fact, it is very simple to publish the two original hidden methods.
Next we will test the project: Create a WinForm project. First, we need to reference our own DataGrid Control. The method is as follows: Right-click the toolbox and select Add/Remove items, use browsing to find the dll under the directory where the project is located and add it to the toolbox. Bind the data to a custom DataGrid using the following method:
SqlConnection conn = new SqlConnection ("server = (local); database = northwind; uid = sa; pwd = ;");
SqlDataAdapter da = new SqlDataAdapter ("SELECT * FROM Orders", conn );
DataSet ds = new DataSet ();
Da. Fill (ds );
This. grdSource. DataSource = ds. Tables [0]. DefaultView;
This. grdAim. DataSource = ds. Tables [0]. DefaultView;
GrdSource and grdAim are two custom DataGrid. What we need to do is to scroll the first DataGrid (grdSource) in the same way.
Private CrlDataGrid. CrlDataGrid grdSource;
Private CrlDataGrid. CrlDataGrid grdAim;
What we need to do is to implement vertical synchronization. We declare two VscrollBar objects. In order to be horizontal, we also declare two horizontal scroll bar objects. VScrollBar m_SourceVScroll;
VScrollBar m_AimVScroll;
HScrollBar m_AimHScroll;
HScrollBar m_SourceHScroll;
We will find their corresponding scroll bar objects in the two custom DataGrid, push the events of these scroll bars into the heap, and add an event handler for them. The Code is as follows:
Public void addEventHandler (){
Foreach (Control ctrl in this. grdSource. Controls ){
If (ctrl. GetType (). Name = "VScrollBar "){
This. m_SourceVScroll = (VScrollBar) ctrl;
Break;
}
}
Foreach (Control ctrl in this. grdAim. Controls ){
If (ctrl. GetType (). Name = "VScrollBar "){
This. m_AimVScroll = (VScrollBar) ctrl;
Break;
}
}
This. m_SourceVScroll.Scroll + = new ScrollEventHandler (m_SourceVScroll_Scroll );
This. m_AimVScroll.Scroll + = new ScrollEventHandler (m_AimVScroll_Scroll );

// ============================================= ====

Foreach (Control ctrl in this. grdSource. Controls ){
If (ctrl. GetType (). Name = "HScrollBar "){
This. m_SourceHScroll = (HScrollBar) ctrl;
Break;

}

}
Foreach (Control ctrl in this. grdAim. Controls ){
If (ctrl. GetType (). Name = "HScrollBar "){
This. m_AimHScroll = (HScrollBar) ctrl;
Break;

}

}
This. m_AimHScroll.Scroll + = new ScrollEventHandler (m_AimHScroll_Scroll );
This. m_SourceHScroll.Scroll + = new ScrollEventHandler (m_SourceHScroll_Scroll );
}

Next we will call this method in the constructor as follows:

Public Form1 (){

InitializeComponent ();
This. addEventHandler ();
}
Finally, add the event handler function as follows:
Private void m_SourceVScroll_Scroll (object sender, ScrollEventArgs e ){
This. m_AimVScroll.Value = this. m_SourceVScroll.Value;
This. grdAim. crlGridVScrolled (sender, e );
}

Private void m_AimVScroll_Scroll (object sender, ScrollEventArgs e ){

This. m_SourceVScroll.Value = this. m_AimVScroll.Value;

This. grdSource. crlGridVScrolled (sender, e );

}

 

Private void m_AimHScroll_Scroll (object sender, ScrollEventArgs e ){

This. m_SourceHScroll.Value = this. m_AimHScroll.Value;

This. grdSource. crlGridHScrolled (sender, e );

}

 

Private void m_SourceHScroll_Scroll (object sender, ScrollEventArgs e ){

This. m_AimHScroll.Value = this. m_SourceHScroll.Value;

This. grdAim. crlGridHScrolled (sender, e );

 

}
The above are the Event Handlers for horizontal and vertical scrolling respectively. So far, the synchronization of the two DataGrid is complete, and compilation and running can achieve the expected goal at the same time!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.