GridHScrolled Listens for the horizontal scrollbar's scroll event.
GridVScrolled Listens for the vertical scrollbar's scroll event.
要得就是它。好了那现在就开始些我们自己的DataGrid吧。首先需要创建一个解决方案,其中有两个工程,一个Windows控件库项目和WinForm项目,前者是我们的写的DataGrid控件后者则是测试控件的工程。创建Windows用户空间会有一个默认的类,删了或者修改他的名字为crlDataGrid(你自己随便叫好了)。我们修改它的继承关系,让他从DataGrid继承。如下所示:
public class CrlDataGrid : System.Windows.Forms.DataGrid。
这样我们就可以用我们自己的DataGrid公开上面提到的两个方法了。如下所示:
public void crlGridVScrolled(object sender,ScrollEventArgs e){
this.GridVScrolled(sender,e);
}
public void crlGridHScrolled(object sender,ScrollEventArgs e){
this.GridHScrolled(sender,e);
}
到此,我们的控件就完成了。其实很简单就是公开了那两个原来隐藏的方法。
接下来就是测试项目:我们新建一个WinForm工程。首先我们需要引用我们自己的DataGrid控件,方法如下:在工具箱里使用鼠标右键选择添加/移除项,使用浏览找到我们刚才工程所在目录下面的dll之后添加到工具箱里。用如下的方法把数据绑定到我们自定义个DataGrid上面:
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和grdAim是两个自定义DataGrid,我们要做的就是当第一个DataGrid(grdSource)滚动的时候第二个也以相同的方式滚动。
private CrlDataGrid.CrlDataGrid grdSource;
private CrlDataGrid.CrlDataGrid grdAim;
下面我们要做的是实现垂直方向的同步我们申明两个VscrollBar对象,为了也可以横向的拖动,我们还声明两个水平的滚动条对象。 VScrollBar m_SourceVScroll;
VScrollBar m_AimVScroll;
HScrollBar m_AimHScroll;
HScrollBar m_SourceHScroll;
我们将在两个自定义DataGrid中找到他们对应的滚动条对象,同时给这些滚动条的事件压入堆中,同时为他们添加事件处理程序,代码如下:
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);
//================== 添加水平 ======================





























接下来我们要在构造函数中调用这个方法如下所示:
public Form1(){
InitializeComponent();
this.addEventHandler();
}
最后就是添加事件处理函数如下所示:
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);
}
上面分别是水平滚动和垂直滚动的事件处理程序。到此为止这种两个DataGrid的同步就完成了,编译运行可以通过同时也达到了预期的目的!