DataGridView Events


.NET Framework Class Library
DataGridView..::.CellValuePushed Event

Occurs when the VirtualMode property of the DataGridView control is true and a cell value has changed and requires storage in the underlying data source.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Event CellValuePushed As DataGridViewCellValueEventHandler
Visual Basic (Usage)
Dim instance As DataGridView
Dim handler As DataGridViewCellValueEventHandler

AddHandler instance.CellValuePushed, handler
C#
public event DataGridViewCellValueEventHandler CellValuePushed
Visual C++
public:
 event DataGridViewCellValueEventHandler^ CellValuePushed {
    void add (DataGridViewCellValueEventHandler^ value);
    void remove (DataGridViewCellValueEventHandler^ value);
}
JScript
JScript does not support events.
Remarks

Use this event in virtual mode to update a custom data store with user-specified data. Handle the CellValueNeeded event to retrieve values from the data store for display in the control.

For more information about virtual mode, see Virtual Mode in the Windows Forms DataGridView Control.

For more information about handling events, see Consuming Events.

Examples

The following code example handles the CellValuePushed event to store updates and new entries in a data store object. This example is part of a larger example available in the VirtualMode reference topic.

Visual Basic
#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)
C#
#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>();
Visual C++
#pragma region Data store maintance

    void VirtualConnector::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 = gcnew Int32(store->default[e->RowIndex]); 
        }
        else if (newRowNeeded && e->RowIndex == numberOfRows)
        {
            if (dataGridView1->IsCurrentCellInEditMode)
            {
                e->Value = initialValue;
            }
            else
            {
                // Show a blank e if the cursor is just loitering
                // over(the) last row.
                e->Value = String::Empty;
            }
        }
        else
        {
            e->Value = e->RowIndex;
        }
    }

    void VirtualConnector::dataGridView1_CellValuePushed
        (Object^ sender, DataGridViewCellValueEventArgs^ e)
    {
        String^ value = e->Value->ToString();
        store[e->RowIndex] = Int32::Parse(value, 
            CultureInfo::CurrentCulture);
    }
#pragma endregion
Platforms

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Community Content

Xentrax [Vyacheslav Lanovets]
Forcing CellValuePushed to be invoked without user leaving the cell

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1857068&SiteID=1

"DataGridView.CellValuePushed event is raised when the VirtualMode property of the DataGridView control is true and a cell value *has changed* and requires storage in the underlying data source, when the current cell is still in edit mode, by default, the cell value is not really changed, the cell is in *DIRTY* state, the changes will be committed after the current cell is lost focus, however, we can handle the CurrentCellDirtyStateChanged event to force the current cell to commit its changes while it's still in edit mode." (c) Zhi-Xin Ye - MSFT

Tags :

Jordan At Kawa
Datagridview virtualmode CellValuePushed can contain stale data
This information should be included in the remarks section.

Details:
The simple version is that you must make sure that no code paths attempt to access DataGridView1._.Value as this will invalidate the user entered text.

The issue was documented and solved here
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/702f3ad7-9109-4755-8377-35aeff0381a4/

An Excerpt:
I can reproduce your problem, I am not sure this one is the same as yours.
Look at this code, it works great.
void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
dt.Rows[e.RowIndex][0] = e.Value;
}

Then I changed a little,

void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
string s = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
dt.Rows[e.RowIndex][0] = e.Value;
}

I have only added a line of code to read the value of the current cell. In this situation, e.Value show the previous value. Through there is no documentation show why, but base on my analyse, when you call the "dataGridView1[e.ColumnIndex, e.RowIndex].Value", it fire the CellValueNeeded and reload the data from the source. So the e.Value has been rollback to the previous.

dt is the DataTable that I have used to load data and write data to.

If you have any doubt, please feel free to tell me.

Sincerely,
Kira Qian
Tags :

Page view tracker