How to synchronize two identical DataGrid
Yesterday, I saw some people in 9CBS (100) asking this question, there is a little itchy, and I look at the problem carefully. In fact, I have been a thing I did for a long time. Today, I just study it. Some gains, take it out and share it.
Problem Description: How to synchronize two DataGrids synchronously in WinForm.
Problem Analysis: The first thing to get this is to rewrite the scroll method, but I think about workload, so I want to do a hand feet at the FORM level, but I have been unsuccessful (I have I haven't seen it online. " ). Under the helpless look, you can see the two protective methods that can be seen from DataGrid to see the list of DataGrid members:
Gridhscrolled Listens for The Horizontal Scrollbar's Scroll Event.
Gridvscrolled Listens for The Vertical Scrollbar's Scroll Event.
Obviously this two methods of listening to the scroll strip event, it is to be it, Microsoft is really great (heart in the heart). Ok, then start some of our own DataGrid. First of all, you need to create a solution, with two projects, a Windows control library project and WinForm project, the former is the latter of the DataGrid control of our test control. Creating a Windows user space has a default class, deleting or modifying his name for CRLDATAGRID (you call it casually). We modify its inheritance and let him inherit from DataGrid. As follows:
Public class crldataGrid: system.windows.forms.dataGrid.
This way we can use our own DataGrid to disclose 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 open the two ways to hide.
Next is the test item: We create a new WinForm project. First we need to quote our own DataGrid control, the method is as follows: Use the mouse button in the toolbox to select the Add / Transfer entry, use the browse to find the DLL we just under the directory of the project to add to the toolbox. Bind the data to our custom DataGrid on the following:
SqlConnection conn = new sqlconnection ("server = 192.192.192.1; database = northwind; uid = sa; pwd =;");
SqlDataAdapter Da = New Sqldataadapter ("Select * from Orders", CONN);
DataSet DS = New DataSet ();
Da.fill (DS);
This. grdsource.dataserce = ds.tables [0] .defaultview;
This. Grdaim.datasource = DS.TABLES [0] .defaultView; where GrDsource and Grdaim are two custom DataGrid, what we have to do is when the first DataGrid (GrDsource) is scrolling the second same way scroll.
Private cadatagrid.crldataGrid grdsource;
Private CRLDATAGRID.CRLDATAGRID GRDAIM;
Below we have to do it, to achieve the vertical direction, we have the two vScrollBar objects, and we also declare two horizontal scroll bars.
Vscrollbar m_sourcevscroll;
Vscrollbar m_aimvscroll;
HScrollBar M_AIMHSCROLL;
HScrollbar m_sourcehscroll;
We will find their corresponding scroll bars objects in two custom DataGrids, and simultaneously press the events of these scroll bars into the stack, and add event handlers to 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);
/ / ================== Add level =========================
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 must call this method in the constructor as follows:
Public form1 () {
//
// Windows Form Designer Support
//
InitializationComponent ();
This.AddeventHandler ();
}
Finally, the addition of the event handler is as follows:
Private void m_sourcevscroll_scroll (Object Sender, Scrolleventargs E) {
THIS.M_AIMVSCROLL.VALUE = this.m_sourcevscroll.value;
This.Grdaim.crgridvscrolled (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 is horizontal scrolling and vertically scrolling event handler.
Here, this two DataGrid is completed, and the compilation run can also achieve the purpose of the expected expectation!
The articles written in a hurry hopes that netizens can criticize and correct, and you can write a message or you can write wu_jian830@hotmail.com. You can contact me if you need source code!