Click here to Skip to main content
13,249,813 members (67,036 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 18 Feb 2010

Custom VScrollbar to make DataGrid finger flickable

, 18 Feb 2010
Rate this:
Please Sign up or sign in to vote.
A custom VScrollbar to make DataGrid finger flickable.


The VScrollbar control solves the problem of the DataGrid in the Compact Framework not being able to scroll from finger flicking like in the ListView or the ListBox.


It was surprising to me that the DataGrid control is not capable of doing finger flicking. Maybe most of the time, if we need that functionality, we should use the ListView control. But I need the functionality on my DataGrid for a project that I'm working on now; since I have done a lot of customization on the grid, I can't really go back to the ListView. I thought I could easily add the functionality to the grid myself, and I was right, it wasn't hard. But the problem is that there is no way to set the position of the native scrollbar of the grid. So I have a grid that scrolls on finger flicking but the scrollbar position doesn't change. Since there is no way to set the scrollbar position programmatically, I need to "hide" the scrollbar. And that's the reason for this scrollbar control.

Using the Code

The code to use the control is pretty simple. Currently, the control does not have a parameterless constructor. So, we can not do the drag and drop thing. But, all it needs is really just the instance of the DataGrid to attach to and the datasource for the DataGrid.

private void Form1_Load(object sender, EventArgs e)
    DataTable dt = GetData();
    var scrollBar = new GridVScrollBar(this.dataGrid1, dt);
    //This line is not needed if you don't need to show the scrollbar


Currently, the second input parameter needs to be of type DataTable or IList. I think it can be made DataTable and IEnumerable with a little extra code.

private void SetVisibleItems(int startIndex)
    object displaySource = new object();
    var endIndex = VisibleRowCount + startIndex;

    if (_controlDataSource is DataTable)
        var sdt = _controlDataSource as DataTable;
        var dt = sdt.Clone();
        for (int i = startIndex; i < endIndex; i++)
            if (i < ItemCount && i >= 0)
        displaySource = dt;
    else if (_controlDataSource is IList)
        var sdt = _controlDataSource as IList;
        var list = (IList)Activator.CreateInstance(_controlDataSource.GetType());
        for (int i = startIndex; i < endIndex; i++)

    _dataGrid.DataSource = displaySource;
    //Or one could raise an event such as DisplaySourceReady here to 
    //so that any extra can be handled before the grid refreshes

We should probably set EnableAutoScroll to false for grids that have a small visible window relative to the row height. Because the auto-scroll logic is not working well, this scenario has not come up yet.

private void Form1_Load(object sender, EventArgs e)
    DataTable dt = GetData();
    var scrollBar = new GridVScrollBar(this.dataGrid1, dt);
    scrollBar.EnableAutoScroll = false;

Future Improvements

Please post your code if you make any nice improvements!

  • Make the datasource less restrictive.
  • Make the control work with more than just DataGrid.
  • Improve the auto-scroll logic. This is a bit difficult and time consuming. Arctan (speed) is probably is good enough alone.


  • Feb. 18, 2010 -- First draft.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Oliver Wyman Groups
United States United States
Education: Masters in Applied mathematics

MCP in
MCP in SQL Server 2008 Implementation.

Working Experience In .Net since 2005

You may also be interested in...


Comments and Discussions

QuestionIt Can Be Horizantal too ? Pin
oguzkaygun12-Dec-13 23:28
memberoguzkaygun12-Dec-13 23:28 
QuestionIf _scrollValue < Me.Minimum Pin
mwalsh628-May-12 7:34
membermwalsh628-May-12 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 18 Feb 2010
Article Copyright 2010 by Ming_Lu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid