1
votes

I have a DataGridview.it has a dgv_scroll event. Here in the horizontal scrolling (on moving the scroll bar) i need that it should flush off exactly the width of the column.Like it does when we click at the end arrows(both at right end end left end) of Datagridview scroll bar.

1

1 Answers

2
votes

in the DataGridView scroll event add this

if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
    e.NewValue = GetColumnOffset(e.NewValue); 
}

then define method

private int GetColumnOffset(int offset)
{
    int start = 0, end = 0;
    DataGridViewColumnCollection Columns = dgvBudgetPeriods.Columns;
    foreach (var column in Columns.Cast<DataGridViewColumn>().Where(c => !c.Frozen))
    {
        end = start + column.Width;
        if (start <= offset && offset < end)
        {
            break;
        }
        start = end;
    }
    return start == offset ? offset : end;
}