Compartilhar via


DataGrid Q&A

I received an e-mail today that contained a question that I've seen posted across several different newsgroups and thought that others would find my response useful.

Q: Is it possible to have a single datagrid split in two parts, with the same vertical scrollbar? If so, how can I create this?

A: You can’t have two different grids in one DataGrid control, but it’s possible to achieve the effect you’re looking for using two DataGrids. You can download the sample solution that I conjured up to demonstrate this.

The ScrollLessDataGrid is a custom grid in which the scroll bars are hidden. Since setting the visibility of the protected scroll bars only hides the scroll bars temporarily, the best way to hide the scroll bars is to just set the width and height of the vertical and horizontal scroll bars respectively to zero. I’ve also added an internal method named UpdateScrollValues that accepts an object of type ScrollData, which is a custom data structure that holds the scroll values. Those values are wrapped up in ScrollEventArgs objects and are used to invoke the GridHScrolled and GridVScrolled methods to update the scroll positions on the grid.

Since there is no way to obtain the scroll bar values outside of the DataGrid, I had to subclass the DataGrid once again. The only thing added to the CustomScrollDataGrid class is an internal property named CurrentScrollValues that returns a ScrollData object containing the current scroll values. Those values are obtained through the value property of the horizontal and vertical scroll bar objects.

On the form, I subscribed to the CustomScrollDataGrid’s Scroll event. Since there’s no way to tell which scroll bar was scrolled, I decided to just store both scroll bar values in a ScrollData object. This object is shipped to the ScrollLessDataGrid by way of the UpdateScrollValues method that I mentioned earlier.

Comments

  • Anonymous
    January 18, 2005
    Hey Chris, one of the Sells brothers pointed me in your direction. Nice bit of work there.

    Here is a problem I'm wresting with that must have been asked once or twice before:

    How can I extend the DataGrid's furthest right column so that the column (or, at least, the column header) extends all the way to the right edge of the grid when the furthest right column is visible?

    At work, I set ComponentOne FlexGrid's ExtendLastCol property to true, which works in all cases, but I'm working on a .NET podcasting client on my own that I want to make open source; therefore, it would be stealing in more ways than one to use their grid.

    Any suggestions on how to extend the DataGrid to behave this way?
  • Anonymous
    January 18, 2005
    See my entry at http://blogs.msdn.com/csano/archive/2005/01/17/354901.aspx.
  • Anonymous
    January 29, 2005
    where's is the download for this sample. I'm trying to do the same thing with the scroll bars and am close, but not quite right.
  • Anonymous
    January 29, 2005
    Click on the download link in the first paragraph of the answer. You couldn't see it before because the link color was the same as the text color. My fault. That has been fixed.
  • Anonymous
    June 13, 2009
    PingBack from http://barstoolsite.info/story.php?id=211