Title How to synchronize two identical DataGrid cuike519 (original) Keywords DataGrid, Scroll, synchronization
How to synchronize two identical DataGrid I saw someone at 9CBS yesterday (100) asking this question, there is a little itchy, and I look at the problem carefully. In fact, I have been a thing I have done for a long time. Today is just a study on the weekend, some gains, and share it with you. 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. " ). In desperation I had to see if you can start from the DataGrid, take a look at the list of members of the DataGrid can see two such protective methods:.. GridHScrolled Listens for the horizontal scrollbar's scroll event GridVScrolled Listens for the vertical scrollbar's scroll event it is clear that Two methods of listening to scroll bar events, it is to be it, Microsoft is really great (heart in my 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 shown below: 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.datasource = 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 to scroll in the same way when the first DataGrid (GrDsource is scrolling.
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 the source code.