简体   繁体   中英

syncing custom horizontal scrollbar's scroll to datagridview's scroll

I have a datagridviews and two custom scrollbars - one vertical and one horizontal scrollbar.

I am resizing the scrollbars when datagridview is populated

scrollBarEx1.Maximum = dataGridView1.RowCount;
scrollBarEx3.Maximum = dataGridView1.ColumnCount;

This is the scroll event for datagridview1

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                scrollBarEx1.Value = e.NewValue;
            }
            else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                scrollBarEx3.Value = e.NewValue;
            }
        }

And here under is the code for scroll event for both scrollbars

private void scrollBarEx1_Scroll(object sender, ScrollEventArgs e)
        {
            dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height = e.NewValue;
}

private void scrollBarEx3_Scroll(object sender, ScrollEventArgs e)
        {
            dataGridView1.Columns[dataGridView1.FirstDisplayedScrollingColumnIndex].Width = e.NewValue;
        }

However only vertical scroll is working. Horizontal scroll does scroll, but the grid does not scroll with it. Help please.

When you have a custom scroll you always set the values of maximum, largechange and small change as follows:

Horizontal Scroll

Maximum = total width
LargeChange = control width
SmallChange = 10% of total width, in this case the width of first column

Vertical Scroll

Maximum = total height
LargeChange = control height
SmallChange = 10% of total height, in this case the height of first row

So for horizontal:

//set these values probably at form load event
int totalwidth = dataGridView1.RowHeadersWidth + 1;

for( int i = 0; i < dataGridView1.Columns.Count; i++ ) {
    totalwidth += dataGridView1.Columns[ i ].Width;
}

hScrollBar1.Maximum = totalwidth;
hScrollBar1.LargeChange = dataGridView1.Width;
hScrollBar1.SmallChange= dataGridView1.Columns[ 0 ].Width;

and

private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
    if( e.ScrollOrientation == ScrollOrientation.HorizontalScroll ) {
        hScrollBar1.Value = e.NewValue;
    }
}

private void hScrollBar1_Scroll( object sender, ScrollEventArgs e ) {
    dataGridView1.HorizontalScrollingOffset = e.NewValue;
}

EDIT

for verticall scroll in DataGridView you can't scroll by pixels but by rows only. So

Vertical Scroll

Maximum = total number of rows
LargeChange = number of visible rows, even a small part counts
SmallChange = 1

So for vertical

vScrollBar1.Maximum = dataGridView1.RowCount;
vScrollBar1.LargeChange = dataGridView1.DisplayedRowCount(true);
vScrollBar1.SmallChange = 1;

and

private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
    if( e.ScrollOrientation == ScrollOrientation.VerticalScroll ) {
        vScrollBar1.Value = e.NewValue;
    }
}

private void vScrollBar1_Scroll( object sender, ScrollEventArgs e ) {  
    dataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM