2011-03-07 283 views

回答

7

如果你有dgv1和dgv2,您可以創建類似

dgv1.Scroll += new System.Windows.Forms.ScrollEventHandler(dgv1_Scroll); 

然後,在dgv1_Scroll方法,你可以使用FirstDisplayedScrollingRowIndex屬性:

dgv2.FirstDisplayedScrollingRowIndex = dgv1.FirstDisplayedScrollingRowIndex 

當然,如果DGV的有不同的ammount的行,你需要避免IndexOutOfRange例外通過檢查每個DGV行數。

1

我相信你可以設置一個事件性的情況下,在滾動時A的「值」的變化,改變滾動條B到適當的值也是如此。

(請注意,值是滾動條的屬性,我的意思不是容器內的數據的價值。)

請參閱關於爲更好的參考滾動條的那個屬性下面的MSDN文章:

http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.value.aspx

和類本身 - http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.aspx

1

你可以把DataGridViews在​​和使用此:

public Form1() 
{ 
    InitializeComponent(); 
    panel1.Scroll += new ScrollEventHandler(panel1_Scroll); 
} 

void panel1_Scroll(object sender, ScrollEventArgs e) 
{ 
    panel2.AutoScrollPosition = new Point(0,e.NewValue); 
} 

不幸的是,似乎並沒有DataGridView有此屬性。

0

看看this。當我滾動其中任何一個時,我想同步兩個列表視圖。你可以使用自定義控件實現這一點。代碼就像一個魅力。

-1
_dataGridViewInput.Scroll += new ScrollEventHandler(_dataGridViewInput_Scroll); 
_dataGridViewOutput.Scroll += new ScrollEventHandler(_dataGridViewOutput_Scroll); 

void _dataGridViewInput_Scroll(object sender, ScrollEventArgs e) 
{ 
    this._dataGridViewOutput.FirstDisplayedScrollingRowIndex = this._dataGridViewInput.FirstDisplayedScrollingRowIndex; 
} 

void _dataGridViewOutput_Scroll(object sender, ScrollEventArgs e) 
{ 
    this._dataGridViewInput.FirstDisplayedScrollingRowIndex = this._dataGridViewOutput.FirstDisplayedScrollingRowIndex; 
} 
+0

你應該解釋它爲什麼可以工作 – 2015-07-31 01:02:02

+0

Just Rowindex Property。很顯然,當行索引不同時需要編寫代碼來處理異常。 如果你在尋找複雜解決方案 - >閱讀http://www.codeproject.com/Articles/39244/Scroll-Synchronization代碼(非常野趣) – 2015-08-01 06:23:22

1

使用HorizontalScrollingOffset(或VerticalScrollingOffset)。

this.dataGridViewDataSample.HorizontalScrollingOffset