locked
Virtual mode in datagridview RRS feed

  • Question

  • is there any way i can use a virtual mode for a datagridview bound to a datasource.
    I used the data source wizard to create my connections for the datagridview and now need to implement some sort of caching performance will be a factor in this application.
    any help will be helpful
    Wednesday, August 16, 2006 10:41 PM

Answers

  • Hi,
     DataGridView FAQ wrote:

    With virtual mode, you can manage the interaction between the DataGridView control and a custom data cache. To implement virtual mode, set the VirtualMode property to true and handle one or more of the events described in this topic. You will typically handle at least the CellValueNeeded event, which enables the control look up values in the data cache.

    Virtual-Mode Events

    If your data is read-only, the CellValueNeeded event may be the only event you will need to handle. Additional virtual-mode events let you enable specific functionality like user edits, row addition and deletion, and row-level transactions.

     

    Some standard DataGridView events (such as events that occur when users add or delete rows, or when cell values are edited, parsed, validated, or formatted) are useful in virtual mode, as well. You can also handle events that let you maintain values not typically stored in a bound data source, such as cell ToolTip text, cell and row error text, cell and row shortcut menu data, and row height data.

     

    The following events occur only when the VirtualMode property is set to true.

    Event

    Description

    CellValueNeeded

    Used by the control to retrieve a cell value from the data cache for display. This event occurs only for cells in unbound columns.

    CellValuePushed

    Used by the control to commit user input for a cell to the data cache. This event occurs only for cells in unbound columns.

    Call the UpdateCellValue method when changing a cached value outside of a CellValuePushed event handler to ensure that the current value is displayed in the control and to apply any automatic sizing modes currently in effect.

    NewRowNeeded

    Used by the control to indicate the need for a new row in the data cache.

    RowDirtyStateNeeded

    Used by the control to determine whether a row has any uncommitted changes.

    CancelRowEdit

    Used by the control to indicate that a row should revert to its cached values.


    If you would like to see some code snippet, this link will direct you to download a DataGridView sample in which there is a virtual mode sample.

    Hope this gives you some hint.
    Thursday, August 17, 2006 1:32 AM

All replies

  • Hi,
     DataGridView FAQ wrote:

    With virtual mode, you can manage the interaction between the DataGridView control and a custom data cache. To implement virtual mode, set the VirtualMode property to true and handle one or more of the events described in this topic. You will typically handle at least the CellValueNeeded event, which enables the control look up values in the data cache.

    Virtual-Mode Events

    If your data is read-only, the CellValueNeeded event may be the only event you will need to handle. Additional virtual-mode events let you enable specific functionality like user edits, row addition and deletion, and row-level transactions.

     

    Some standard DataGridView events (such as events that occur when users add or delete rows, or when cell values are edited, parsed, validated, or formatted) are useful in virtual mode, as well. You can also handle events that let you maintain values not typically stored in a bound data source, such as cell ToolTip text, cell and row error text, cell and row shortcut menu data, and row height data.

     

    The following events occur only when the VirtualMode property is set to true.

    Event

    Description

    CellValueNeeded

    Used by the control to retrieve a cell value from the data cache for display. This event occurs only for cells in unbound columns.

    CellValuePushed

    Used by the control to commit user input for a cell to the data cache. This event occurs only for cells in unbound columns.

    Call the UpdateCellValue method when changing a cached value outside of a CellValuePushed event handler to ensure that the current value is displayed in the control and to apply any automatic sizing modes currently in effect.

    NewRowNeeded

    Used by the control to indicate the need for a new row in the data cache.

    RowDirtyStateNeeded

    Used by the control to determine whether a row has any uncommitted changes.

    CancelRowEdit

    Used by the control to indicate that a row should revert to its cached values.


    If you would like to see some code snippet, this link will direct you to download a DataGridView sample in which there is a virtual mode sample.

    Hope this gives you some hint.
    Thursday, August 17, 2006 1:32 AM
  • i had a look at this before and tried the cellvalueneeded event but it doesnt get triggered and neither is newrowneeded. I had set my virtual mode to true even. does this event get triggered only with a unbound grid?
    Thursday, August 17, 2006 2:55 AM
  • Hi,
    I tried the sample from the link I posted above and the cellvalueneeded does get triggered in that example. As to your specific problem, maybe some other details violate the basic rule.
    Thursday, August 17, 2006 6:36 AM
  • It does get triggered for bound and unbound controls. What you're referring to is mixed-mode : bound and virtual.  I was also having difficulty getting this NewRowNeeded  event and didn't get it consistently until also trapping the UserAddedRow event.  A coinsidence maybe, but I couldn't find anything in my searching that mentions any rules for NewRowNeeded  getting or not getting called.
    Wednesday, March 14, 2007 7:09 PM
  • Another requirement to trigger cellvalueneed event is to set datagridview.rowcount.

    Wednesday, September 26, 2007 3:26 PM
  • datagridview.Row.Count is a getter .....
    How do you do this ?
    Thursday, October 18, 2007 4:16 PM
  • I'm having the same or similar problem with VirtualMode, and another problem as well. 

    I've read a couple of articles in MSDN and on the web about using VirtualMode and the CellValue* events of the DataGridView control to implement the displaying/editing of very large amounts of data using just-in-time data retrieval/storage of a page of data at a time, but those articles seem to be in error.

    You're supposed to set VirtualMode to true, set up handlers for CellValueNeeded and such, and then set the RowCount (not Rows.Count, but RowCount) property.  The problem is, even when the DGV has VirtualMode set to true, the CellValueNeeded event doesn't get triggered when the user scrolls - it gets triggered for each cell of each row, for all the rows, as soon as RowCount is set.  All the rows.  So it forces the up front retrieval of all the rows, and stores all that data in the form of DataGridViewRow's with ItemArray elements.  I have verified this by writing output to Debug from the CellValueNeeded handler. If you set RowCount to less than the total number of rows in the entire query result set (for example), then the scroll bar doesn't get set up to allow the user to scroll through the whole result set.

    There must be some page size property or something that I'm missing?  Or a property that allows you to set a mode that causes it to trigger the CellValueNeeded event just prior to displaying a cell, rather than when RowCount is set?  Or, there must be some other way of doing this, like directly addressing the scroll bar properties and events, and ignoring CellValueNeeded events, and instead servicing scrollbar events, and forcibly setting the data in the displayed in cells when the user scrolls or something; but it seems to me that that would be slow.

    Another problem I have with VirtualMode is that if VirtualMode is set to true, support for allowing the user to sort the data by a column is lost.  Setting AllowUserToOrderColumns after setting VirtualMode to true doesn't help.  Once VirtualMode is set to true, no longer can the user sort by a column, no matter what the setting of AllowUserToOrderColumns is.  The arrows don't appear in the header cells, nothing happens when the user clicks on the header of a column, etc. - it is as though AllowUserToOrderColumns were set to false, even if it is set to true.


    Tuesday, April 15, 2008 11:46 PM
  • Hi.  You've probably figured this out already, but one cause for all the CellValueNeeded events getting fired upon setting RowCount is setting the grid's AutoSizeColumnMode to AllCells (or manually calling AutoSizeColumns() with no arguments or with AllCells as an argument).  If you tell the grid to adjust its layout based on the contents of all cells, it'll have to go get the contents of all cells.
    Monday, April 28, 2008 2:14 PM