user1213556
user1213556

Reputation: 1

horizontal scrolling similar to that of excel using DataGridView

Is possible for a DataGridView to scroll in a staggered manor (similar to that of excel) horizontally.

I don't need to scroll along the column cell(continuously) instead when the scrolling reaches cell boundary, grid should scroll next column cell.

Upvotes: 0

Views: 458

Answers (2)

Michael Barabash
Michael Barabash

Reputation: 103

You can override OnScroll method and calculate next offset

    protected override void OnScroll(ScrollEventArgs e)
    {
        if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
        {
            e.NewValue = GetColumnOffset(e.NewValue);;
        }
        base.OnScroll(e);
    }

    private int GetColumnOffset(int offset)
    {
        int start = 0, end = 0;
        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;
    }

Upvotes: 1

Vildan
Vildan

Reputation: 1994

Try to use FirstDisplayedScrollingColumnIndex property. Should help. If to set FirstDisplayedScrollingColumnIndex it jumps to the beginning of specified column index.

Something like:

protected override void OnScroll(ScrollEventArgs e) {
    base.OnScroll(e);
    this.FirstDisplayedScrollingColumnIndex = Convert.ToInt32(this.HorizontalScrollBar.Value / this.Width); }

Upvotes: 0

Related Questions