DataGridView.CellValueNeeded 이벤트

정의

DataGridView 컨트롤의 VirtualMode 속성이 true이고, DataGridView에서 셀을 서식 지정하고 표시하기 위해 셀에 대한 값이 필요할 때 발생합니다.

public:
 event System::Windows::Forms::DataGridViewCellValueEventHandler ^ CellValueNeeded;
public event System.Windows.Forms.DataGridViewCellValueEventHandler CellValueNeeded;
public event System.Windows.Forms.DataGridViewCellValueEventHandler? CellValueNeeded;
member this.CellValueNeeded : System.Windows.Forms.DataGridViewCellValueEventHandler 
Public Custom Event CellValueNeeded As DataGridViewCellValueEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 CellValueNeeded 이벤트를 처리하여 셀을 양의 정수로 채웁니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 VirtualMode 참조 항목입니다.

#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
#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>();
#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)

설명

이 이벤트를 가상 모드에서 사용하여 행이 공유되지 않도록 하지 않고 사용자 지정 데이터 저장소의 데이터로 셀을 채웁니다. 행 공유에 대한 자세한 내용은 Windows Forms DataGridView 컨트롤의 크기를 조정하는 최선의 방법을 참조하세요. 가상 모드에 대한 자세한 내용은 Windows Forms DataGridView 컨트롤에서 가상 모드를 참조하세요.

사용자 지정 데이터 저장소에 사용자 지정 값을 추가하려면 이벤트를 처리합니다 CellValuePushed .

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보