开发者

C# windows form 2 gridviews with synced scroll

I am developing an application in which two datagridviews are being populated from different data sources. I would like to have a 开发者_如何学编程single vertical scroll-bar that will make both gridviews work at the same time (scroll up and down together)

can anyone tell me how or direct me to a good tutorial.


If you have dgv1 and dgv2, you can create something like

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

Then, in dgv1_Scroll method, you can use FirstDisplayedScrollingRowIndex property:

dgv2.FirstDisplayedScrollingRowIndex = dgv1.FirstDisplayedScrollingRowIndex

Of course, if dgv's have different ammount of rows, you need to avoid IndexOutOfRange exception by checking each dgv rows count.


Use HorizontalScrollingOffset (or VerticalScrollingOffset).

this.dataGridViewDataSample.HorizontalScrollingOffset


I believe you can set up an event-like scenario, where whenever scrollbar A's "value" changes, change scrollbar B to the appropriate value as well.

(Note that value is a property of a scroll bar, I do not mean the value of the data inside the container.)

Please see the following msdn article regarding that property of a scroll bar for better reference:

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

And the class itself -- http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.aspx


You could put the DataGridViews in Panels and use this:

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

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

Unfortunately it does not seem that DataGridView has this property.


http://www.xs4all.nl/~wrb/Articles_2010/Article_DataGridViewScroll_01.htm

This link shows exactly what I needed and worked fine for me. The only problem I have now is that the datagrids does not have same rows in it. So even when one finishes (no more to scroll) the other must be able to continue.

Any suggestions?


take a look at this. i wanted to sync two listviews when i scroll any of them. u can achieve this using custom controls. code works like a charm.


_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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜