I have three DataGridViews: dgvSTART; dgvFilter; dgvEdit.
I have edited the scroll event for dgvSTART, inline with the answer here , which looks like:
dgvEdit.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
dgvFilter.FirstDisplayedScrollingColumnIndex = dgvSTART.FirstDisplayedScrollingColumnIndex;
This works, but only scrolls the other two DataGridViews when the left hand column of dgvSTART changes (ie the column dissapears/appears off the side of the panel).
Is it possible to make all the datagridviews scroll as one (ignoring the column boundaries)?
I think this is possible using the HorizontalScrollingOffset property of the DataGridView, but my attempt was unsuccessful:
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
}
How about this code ?
dataGridView_Scroll
need to be registered in the constructor of the form or something.
dgvSTART.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dgvFilter.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dgvEdit.Scroll += new ScrollEventHandler(dataGridView_Scroll);
dataGridView_Scroll
is as follows.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.