Proprietà DataGridView.VirtualMode (System.Windows.Forms)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Proprietà DataGridView.VirtualMode

Nota: questa proprietà è stata introdotta con .NET Framework versione 2.0.

Ottiene o imposta un valore che indica se sono state fornite operazioni di gestione dati personalizzate per il controllo DataGridView.

Spazio dei nomi: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Sintassi

Visual Basic - (Dichiarazione)
Public Property VirtualMode As Boolean
Visual Basic (Utilizzo)
Dim instance As DataGridView
Dim value As Boolean

value = instance.VirtualMode

instance.VirtualMode = value
C#
public bool VirtualMode { get; set; }
C++
public:
property bool VirtualMode {
	bool get ();
	void set (bool value);
}
J#
/** @property */
public boolean get_VirtualMode ()

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

JScript
public function get VirtualMode () : boolean

public function set VirtualMode (value : boolean)

Valore proprietà

true se DataGridView utilizza operazioni di gestione dati fornite; in caso contrario, false. Il valore predefinito è false.
Note

La modalità virtuale è progettata per l'utilizzo con archivi dati di grandi dimensioni. Quando la proprietà VirtualMode è true, è necessario creare un controllo DataGridView con un numero stabilito di righe e colonne e quindi gestire l'evento CellValueNeeded per inserire i dati nelle celle. La modalità virtuale richiede l'implementazione di una cache dati sottostante per gestire l'inserimento dei dati, la modifica e l'eliminazione delle celle di DataGridView in base alle azioni dell'utente. Per ulteriori informazioni sull'implementazione della modalità virtuale, vedere Procedura: implementare il modo virtuale nel controllo DataGridView di Windows Form.

Quando il controllo DataGridView è in modalità di associazione, per gestire i valori delle colonne non associate è necessario utilizzare la modalità virtuale. In modalità di associazione, l'ordinamento in base a colonne non associate non è supportato.

Esempio

Nell'esempio di codice riportato di seguito viene utilizzata la modalità virtuale per creare una tabella di valori integer positivi.

Visual Basic
Imports System.IO
Imports System.Collections.Generic
Imports System.Windows.Forms

Public Class VirtualModeDemo
    Inherits System.Windows.Forms.Form

    Dim WithEvents dataGridView1 As New DataGridView

    Public Sub New()

        MyBase.New()

        Text = "DataGridView virtual-mode demo (cell-level commit scope)"

        Controls.Add(dataGridView1)
        dataGridView1.VirtualMode = True
        dataGridView1.AllowUserToDeleteRows = False
        dataGridView1.Columns.Add("Numbers", "Positive Numbers")
        dataGridView1.Rows.AddCopies(0, initialSize)

    End Sub

    Dim newRowNeeded As Boolean

    Private Sub dataGridView1_NewRowNeeded(ByVal sender As Object, _
        ByVal e As DataGridViewRowEventArgs) _
        Handles dataGridView1.NewRowNeeded

        newRowNeeded = True
    End Sub

    Const initialSize As Integer = 5000000
    Dim numberOfRows As Integer = initialSize

    Private Sub dataGridView1_RowsAdded(ByVal sender As Object, _
        ByVal e As DataGridViewRowsAddedEventArgs) _
        Handles dataGridView1.RowsAdded

        If newRowNeeded Then
            newRowNeeded = False
            numberOfRows = numberOfRows + 1
        End If
    End Sub

#Region "data store maintance"
    Const initialValue As Integer = -1

    Private Sub dataGridView1_CellValueNeeded(ByVal sender As Object, _
        ByVal e As DataGridViewCellValueEventArgs) _
        Handles dataGridView1.CellValueNeeded

        If store.ContainsKey(e.RowIndex) Then
            ' Use the store if the e value has been modified 
            ' and stored.
            e.Value = store(e.RowIndex)
        ElseIf newRowNeeded AndAlso e.RowIndex = numberOfRows Then
            If dataGridView1.IsCurrentCellInEditMode Then
                e.Value = initialValue
            Else
                ' Show a blank value if the cursor is just resting
                ' on the last row.
                e.Value = String.Empty
            End If
        Else
            e.Value = e.RowIndex
        End If
    End Sub

    Private Sub dataGridView1_CellValuePushed(ByVal sender As Object, _
        ByVal e As DataGridViewCellValueEventArgs) _
        Handles dataGridView1.CellValuePushed

        store.Add(e.RowIndex, CInt(e.Value))

    End Sub
#End Region

    Dim store As System.Collections.Generic.Dictionary(Of Integer, Integer) = _
        New Dictionary(Of Integer, Integer)

    Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
        ByVal e _
        As DataGridViewCellValidatingEventArgs) _
        Handles dataGridView1.CellValidating

        Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
        Dim newInteger As Integer

        ' Don't try to validate the 'new row' until finished 
        ' editing since there
        ' is not any point in validating its initial value.
        If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return
        If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
            OrElse newInteger < 0 Then

            e.Cancel = True
            Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

        End If
    End Sub

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New VirtualModeDemo())
    End Sub
End Class

C#
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System;

public class VirtualModeDemo : Form
{
    DataGridView dataGridView1 = new DataGridView();

    public VirtualModeDemo()
        : base()
    {
        Text = "DataGridView virtual-mode demo (cell-level commit scope)";
        dataGridView1.NewRowNeeded +=
            new DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);
        dataGridView1.RowsAdded +=
            new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
        dataGridView1.CellValidating +=
            new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
        dataGridView1.CellValueNeeded +=
            new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
        dataGridView1.CellValuePushed +=
            new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

        Controls.Add(dataGridView1);
        dataGridView1.VirtualMode = true;
        dataGridView1.AllowUserToDeleteRows = false;
        dataGridView1.Columns.Add("Numbers", "Positive Numbers");
        dataGridView1.Rows.AddCopies(0, initialSize);
    }

    bool newRowNeeded;
    private void dataGridView1_NewRowNeeded(object sender,
        DataGridViewRowEventArgs e)
    {
        newRowNeeded = true;
    }

    const int initialSize = 5000000;
    int numberOfRows = initialSize;

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

    #region "data store maintance"
    const int initialValue = -1;

    private void dataGridView1_CellValueNeeded(object sender,
        DataGridViewCellValueEventArgs e)
    {
        if (store.ContainsKey(e.RowIndex))
        {
            // Use the store if the e value has been modified 
            // and stored.            
            e.Value = store[e.RowIndex];
        }
        else if (newRowNeeded && e.RowIndex == numberOfRows)
        {
            if (dataGridView1.IsCurrentCellInEditMode)
            {
                e.Value = initialValue;
            }
            else
            {
                // Show a blank value if the cursor is just resting
                // on the last row.
                e.Value = String.Empty;
            }
        }
        else
        {
            e.Value = e.RowIndex;
        }
    }

    private void dataGridView1_CellValuePushed(object sender,
        DataGridViewCellValueEventArgs e)
    {
        store.Add(e.RowIndex, int.Parse(e.Value.ToString()));
    }
    #endregion

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

    private void dataGridView1_CellValidating(object sender,
        DataGridViewCellValidatingEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].ErrorText = "";
        int newInteger;

        // Don't try to validate the 'new row' until finished 
        // editing since there
        // is not any point in validating its initial value.
        if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
        if (!int.TryParse(e.FormattedValue.ToString(),
            out newInteger) || newInteger < 0)
        {
            e.Cancel = true;
            dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
        }
    }

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new VirtualModeDemo());
    }
}

J#
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

Piattaforme

Windows 98, Windows 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

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0
Vedere anche