Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGridView.VirtualMode Property

Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public:
property bool VirtualMode {
	bool get ();
	void set (bool value);
}
/** @property */
public boolean get_VirtualMode ()

/** @property */
public void set_VirtualMode (boolean value)

public function get VirtualMode () : boolean

public function set VirtualMode (value : boolean)

Not applicable.

Property Value

true if the DataGridView uses data-management operations that you provide; otherwise, false. The default is false.

Virtual mode is designed for use with very large stores of data. When the VirtualMode property is true, you create a DataGridView with a set number of rows and columns and then handle the CellValueNeeded event to populate the cells. Virtual mode requires implementation of an underlying data cache to handle the population, editing, and deletion of DataGridView cells based on actions of the user. For more information about implementing virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control.

You must use virtual mode to maintain the values of unbound columns when the DataGridView control is in bound mode. Sorting by unbound columns in bound mode is not supported.

The following code example code uses virtual mode to create a table of positive integers.

No code example is currently available or this language may not be supported.
import System.IO.*;
import System.Collections.Generic.*;
import System.Windows.Forms.*;
import System.Drawing.*;
import System.*;

public class Virtual extends Form
{
    private DataGridView dataGridView1 = new DataGridView();

    public Virtual()
    {
        set_Text(this.GetType().get_Name());
        dataGridView1.add_NewRowNeeded(new DataGridViewRowEventHandler(
            dataGridView1_NewRowNeeded));
        dataGridView1.add_RowsAdded(new DataGridViewRowsAddedEventHandler(
            dataGridView1_RowsAdded));
        dataGridView1.add_CellValidating(
            new DataGridViewCellValidatingEventHandler(
            dataGridView1_CellValidating));
        dataGridView1.add_CellValueNeeded(
            new DataGridViewCellValueEventHandler(
            dataGridView1_CellValueNeeded));
        dataGridView1.add_CellValuePushed(
            new DataGridViewCellValueEventHandler(
            dataGridView1_CellValuePushed));

        try {
            get_Controls().Add(dataGridView1);
            dataGridView1.set_VirtualMode(true);
            dataGridView1.set_AllowUserToDeleteRows(false);
            dataGridView1.get_Columns().Add("Numbers", "Positive Numbers");
            dataGridView1.get_Rows().AddCopies(0, INITIALSIZE);
        }
        catch (System.Exception ex) {
            MessageBox.Show("Exception occured: " + ex.ToString());
        }
    } //Virtual

    private boolean newRowNeeded;

    private void dataGridView1_NewRowNeeded(Object sender,
        DataGridViewRowEventArgs e)
    {
        newRowNeeded = true;
    } //dataGridView1_NewRowNeeded

    private final int INITIALSIZE = 5000000;
    private int numberOfRows = INITIALSIZE;

    private void dataGridView1_RowsAdded(Object sender,
        DataGridViewRowsAddedEventArgs e)
    {
        if (newRowNeeded) {
            newRowNeeded = false;
            numberOfRows = numberOfRows + 1;
        }
    } //dataGridView1_RowsAdded

    #region "data store maintance"
    private final int INITIALVALUE = -1;

    private void dataGridView1_CellValueNeeded(Object sender,
        DataGridViewCellValueEventArgs e)
    {
        if (store.ContainsKey(e.get_RowIndex())) {
            // Use the store if the e value has been modified 
            // and stored.            
            e.set_Value((Int32)store.get_Item(e.get_RowIndex()));
        }
        else {
            if (newRowNeeded && e.get_RowIndex() == numberOfRows) {
                if (dataGridView1.get_IsCurrentCellInEditMode()) {
                    e.set_Value((Int32)INITIALVALUE);
                }
                else {
                    // Show a blank e if the cursor is just loitering
                    // over(the)
                    // last row.
                    e.set_Value("");
                }
            }
            else {
                e.set_Value((Int32)e.get_RowIndex());
            }
        }
    } //dataGridView1_CellValueNeeded

    private void dataGridView1_CellValuePushed(Object sender,
        DataGridViewCellValueEventArgs e)
    {
        store.Add(e.get_RowIndex(), Int32.Parse(e.get_Value().ToString()));
    } //dataGridView1_CellValuePushed
    #endregion

    private Dictionary<int, int> store = new Dictionary<int, int>();

    private void dataGridView1_CellValidating(Object sender,
        DataGridViewCellValidatingEventArgs e)
    {
        int newInteger = 0;
        // Don't try to validate the 'new row' until finished 
        // editing since there
        // is not any point in validating its initial value.
        if (dataGridView1.get_Rows().get_Item(
            e.get_RowIndex()).get_IsNewRow()) {
            return;
        }
        if (!(Int32.TryParse(e.get_FormattedValue().ToString(), newInteger)) 
            || newInteger < 0) {
            e.set_Cancel(true);
        }
    } //dataGridView1_CellValidating

    public static void main(String[] args)
    {
        Application.Run(new Virtual());
    } //main
} //Virtual

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2014 Microsoft