我有三个DataGridViews:dgvSTART; dgvFilter; dgvEdit。
我已经编辑了dgvSTART的滚动事件,内联了答案here,如下所示:
dgvEdit.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
dgvFilter.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
这样可行,但只有当dgvSTART的左侧列发生更改时(即列消失/显示在面板的一侧),才会滚动其他两个DataGridView。
是否可以将所有datagridviews都滚动为一个(忽略列边界)?
我认为这可以使用DataGridView的HorizontalScrollingOffset属性,但我的尝试失败了:
private void dgvSTART_Scroll(object sender, ScrollEventArgs e)
{
//using matching the first column shown
dgvEdit.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
dgvFilter.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
try
{
//trying to use horizontalscrollingoffset
if (e.NewValue < e.OldValue)
{
dgvEdit.HorizontalScrollingOffset = dgvFilter.HorizontalScrollingOffset - 10;
dgvFilter.HorizontalScrollingOffset = dgvFilter.HorizontalScrollingOffset - 10;
}
else
{
dgvEdit.HorizontalScrollingOffset = dgvEdit.HorizontalScrollingOffset + 10;
dgvFilter.HorizontalScrollingOffset = dgvFilter.HorizontalScrollingOffset + 10;
}
}
catch (Exception) { }//do nothing with the exception
}
答案 0 :(得分:4)
这段代码怎么样?
dataGridView_Scroll
需要在表单的构造函数中注册。
dgvSTART.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dgvFilter.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dgvEdit.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dataGridView_Scroll
如下。
void dataGridView_Scroll(object sender, ScrollEventArgs e)
{
DataGridView src;
DataGridView dst1 = null;
DataGridView dst2 = null;
src = (DataGridView)sender;
if (src == dgvSTART)
{
dst1 = dgvFilter;
dst2 = dgvEdit;
}
else if (src == dgvFilter)
{
dst1 = dgvSTART;
dst2 = dgvEdit;
}
else if (src == dgvEdit)
{
dst1 = dgvSTART;
dst2 = dgvFilter;
}
if (dst1 != null && dst2 != null)
{
dst1.HorizontalScrollingOffset = dst2.HorizontalScrollingOffset = src.HorizontalScrollingOffset;
dst1.FirstDisplayedScrollingRowIndex = Math.Min(dst1.RowCount - 1, src.FirstDisplayedScrollingRowIndex);
dst2.FirstDisplayedScrollingRowIndex = Math.Min(dst2.RowCount - 1, src.FirstDisplayedScrollingRowIndex);
}
}