DataGridViewCellContextMenuStripNeededEventArgs DataGridViewCellContextMenuStripNeededEventArgs DataGridViewCellContextMenuStripNeededEventArgs DataGridViewCellContextMenuStripNeededEventArgs Class

定義

CellContextMenuStripNeeded イベントのデータを提供します。Provides data for the CellContextMenuStripNeeded event.

public ref class DataGridViewCellContextMenuStripNeededEventArgs : System::Windows::Forms::DataGridViewCellEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs : System.Windows.Forms.DataGridViewCellEventArgs
type DataGridViewCellContextMenuStripNeededEventArgs = class
    inherit DataGridViewCellEventArgs
Public Class DataGridViewCellContextMenuStripNeededEventArgs
Inherits DataGridViewCellEventArgs
継承
DataGridViewCellContextMenuStripNeededEventArgsDataGridViewCellContextMenuStripNeededEventArgsDataGridViewCellContextMenuStripNeededEventArgsDataGridViewCellContextMenuStripNeededEventArgs

次のコード例では、DataGridViewCellContextMenuStripNeededEventArgs行の共有を解除せずに、ショートカット メニューを設定するクラス。The following code example uses the DataGridViewCellContextMenuStripNeededEventArgs class to set the shortcut menu without unsharing the row.

ToolStripMenuItem^ wholeTable;
ToolStripMenuItem^ lookUp;
System::Windows::Forms::ContextMenuStrip^ strip;
String^ cellErrorText;

void dataGridView1_CellContextMenuStripNeeded( Object^ /*sender*/,
    DataGridViewCellContextMenuStripNeededEventArgs^ e )
{
    cellErrorText = String::Empty;
    if ( strip == nullptr )
    {
        strip = gcnew System::Windows::Forms::ContextMenuStrip;
        lookUp->Text = L"Look Up";
        wholeTable->Text = L"See Whole Table";
        strip->Items->Add( lookUp );
        strip->Items->Add( wholeTable );
    }

    e->ContextMenuStrip = strip;
}

void wholeTable_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
    dataGridView1->DataSource = Populate( L"Select * from employees", true );
}

DataGridViewCellEventArgs^ theCellImHoveringOver;
void dataGridView1_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ e )
{
    theCellImHoveringOver = e;
}

DataGridViewCellEventArgs^ cellErrorLocation;
void lookUp_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
    try
    {
        dataGridView1->DataSource = Populate( String::Format( L"Select * from employees where {0} = '{1}'", dataGridView1->Columns[ theCellImHoveringOver->ColumnIndex ]->Name, dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value ), true );
    }
    catch ( ... ) 
    {
        cellErrorText = L"Can't look this cell up";
        cellErrorLocation = theCellImHoveringOver;
    }

}

void dataGridView1_CellErrorTextNeeded( Object^ /*sender*/, DataGridViewCellErrorTextNeededEventArgs^ e )
{
    if ( cellErrorLocation != nullptr )
    {
        if ( e->ColumnIndex == cellErrorLocation->ColumnIndex && e->RowIndex == cellErrorLocation->RowIndex )
        {
            e->ErrorText = cellErrorText;
        }
    }
}

DataTable^ Populate( String^ query, bool resetUnsharedCounter )
{
    if ( resetUnsharedCounter )
    {
        ResetCounter();
    }


    // Alter the data source as necessary
    SqlDataAdapter^ adapter = gcnew SqlDataAdapter( query,
        gcnew SqlConnection( L"Integrated Security=SSPI;Persist Security Info=False;"
        L"Initial Catalog=Northwind;Data Source= localhost" ) );
    DataTable^ table = gcnew DataTable;
    adapter->Fill( table );
    return table;
}

Label^ count;
int unsharedRowCounter;
void ResetCounter()
{
    unsharedRowCounter = 0;
    count->Text = unsharedRowCounter.ToString();
}
private ToolStripMenuItem wholeTable = new ToolStripMenuItem();
private ToolStripMenuItem lookUp = new ToolStripMenuItem();
private ContextMenuStrip strip;
private string cellErrorText;

private void dataGridView1_CellContextMenuStripNeeded(object sender,
    DataGridViewCellContextMenuStripNeededEventArgs e)
{
    cellErrorText = String.Empty;

    if (strip == null)
    {
        strip = new ContextMenuStrip();
        lookUp.Text = "Look Up";
        wholeTable.Text = "See Whole Table";
        strip.Items.Add(lookUp);
        strip.Items.Add(wholeTable);
    }
    e.ContextMenuStrip = strip;
}

private void wholeTable_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = Populate("Select * from employees", true);
}

private DataGridViewCellEventArgs theCellImHoveringOver;

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    theCellImHoveringOver = e;
}

private DataGridViewCellEventArgs cellErrorLocation;

private void lookUp_Click(object sender, EventArgs e)
{
    try
    {
        dataGridView1.DataSource = Populate("Select * from employees where " +
            dataGridView1.Columns[theCellImHoveringOver.ColumnIndex].Name + " = '" +
            dataGridView1.Rows[theCellImHoveringOver.RowIndex].
            Cells[theCellImHoveringOver.ColumnIndex].Value + "'",
            true);
    }
    catch (SqlException)
    {
        cellErrorText = "Can't look this cell up";
        cellErrorLocation = theCellImHoveringOver;
    }
}

private void dataGridView1_CellErrorTextNeeded(object sender,
    DataGridViewCellErrorTextNeededEventArgs e)
{
    if (cellErrorLocation != null)
    {
        if (e.ColumnIndex == cellErrorLocation.ColumnIndex &&
            e.RowIndex == cellErrorLocation.RowIndex)
        {
            e.ErrorText = cellErrorText;
        }
    }
}

private DataTable Populate(string query, bool resetUnsharedCounter)
{
    if (resetUnsharedCounter)
    {
        ResetCounter();
    }

    // Alter the data source as necessary
    SqlDataAdapter adapter = new SqlDataAdapter(query,
        new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost"));

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

private Label count = new Label();
private int unsharedRowCounter;

private void ResetCounter()
{
    unsharedRowCounter = 0;
    count.Text = unsharedRowCounter.ToString();
}
Private WithEvents wholeTable As New ToolStripMenuItem()
Private WithEvents lookUp As New ToolStripMenuItem()
Private strip As ContextMenuStrip
Private cellErrorText As String

Private Sub dataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, _
    ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _
    Handles dataGridView1.CellContextMenuStripNeeded

    cellErrorText = String.Empty

    If strip Is Nothing Then
        strip = New ContextMenuStrip()
        lookUp.Text = "Look Up"
        wholeTable.Text = "See Whole Table"
        strip.Items.Add(lookUp)
        strip.Items.Add(wholeTable)
    End If
    e.ContextMenuStrip = strip
End Sub

Private Sub wholeTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles wholeTable.Click
    dataGridView1.DataSource = Populate("Select * from employees", True)
End Sub

Private theCellImHoveringOver As DataGridViewCellEventArgs

Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellMouseEnter

    theCellImHoveringOver = e
End Sub

Private cellErrorLocation As DataGridViewCellEventArgs

Private Sub lookUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lookUp.Click
    Try
        dataGridView1.DataSource = Populate("Select * from employees where " & _
            dataGridView1.Columns(theCellImHoveringOver.ColumnIndex).Name & " = '" & _
            dataGridView1.Rows(theCellImHoveringOver.RowIndex).Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _
            "'", True)
    Catch ex As SqlException
        cellErrorText = "Can't look this cell up"
        cellErrorLocation = theCellImHoveringOver
    End Try
End Sub

Private Sub dataGridView1_CellErrorTextNeeded(ByVal sender As Object, _
            ByVal e As DataGridViewCellErrorTextNeededEventArgs) _
            Handles dataGridView1.CellErrorTextNeeded
    If (Not cellErrorLocation Is Nothing) Then
        If e.ColumnIndex = cellErrorLocation.ColumnIndex AndAlso _
            e.RowIndex = cellErrorLocation.RowIndex Then
            e.ErrorText = cellErrorText
        End If
    End If
End Sub

Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable

    If resetUnsharedCounter Then
        ResetCounter()
    End If

    ' Alter the data source as necessary
    Dim adapter As New SqlDataAdapter(query, _
        New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _
        "Initial Catalog=Northwind;Data Source=localhost"))

    Dim table As New DataTable()
    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)
    Return table
End Function

Private count As New Label()
Private unsharedRowCounter As Integer

Private Sub ResetCounter()
    unsharedRowCounter = 0
    count.Text = unsharedRowCounter.ToString()
End Sub

注釈

CellContextMenuStripNeededのみイベントが発生したときに、DataGridViewコントロールDataSourceプロパティを設定またはそのVirtualModeプロパティはtrueThe CellContextMenuStripNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true.

処理するとき、 CellContextMenuStripNeeded 、イベント ハンドラーで指定したショートカット メニューが表示されるたびにユーザーがセルを右クリックします。When you handle the CellContextMenuStripNeeded event, the shortcut menu that you specify in the handler is shown whenever the user right-clicks a cell. これは、現在の状態またはセルの値によって決定されたショートカット メニューを表示する場合に便利です。This is useful when you want to display shortcut menus determined by the current state or value of a cell.

CellContextMenuStripNeededたびにイベントにも発生の値、DataGridViewCell.ContextMenuStripプロパティが取得プログラムか、ユーザーがセルを右クリックしたとき。The CellContextMenuStripNeeded event also occurs whenever the value of the DataGridViewCell.ContextMenuStrip property is retrieved, either programmatically or when the user right-clicks the cell.

使用することができます、ColumnIndexRowIndex状態または、セルの値を決定し、この情報を使用して設定するプロパティ、ContextMenuStripプロパティ。You can use the ColumnIndex and RowIndex properties to determine the state or value of a cell, and use this information to set the ContextMenuStrip property. このプロパティは、セルの値で初期化されますContextMenuStripプロパティで、イベント値より優先されます。This property is initialized with the value of the cell ContextMenuStrip property, which the event value overrides.

処理、CellContextMenuStripNeeded大量のデータ セルを設定したパフォーマンスの低下を回避するために使用する場合、イベントContextMenuStrip複数のセルの値。Handle the CellContextMenuStripNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell ContextMenuStrip value for multiple cells. 詳細については、次を参照してください。 Windows フォーム DataGridView コントロールを拡張するためのベスト プラクティスします。For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

個々 のセル、行を設定してではなく個別の行のショートカット メニューを指定することもContextMenuStripプロパティまたは処理、DataGridViewコントロールのRowContextMenuStripNeededイベント。You can also specify shortcut menus for individual rows rather than individual cells by setting the row ContextMenuStrip property or handling the DataGridView control's RowContextMenuStripNeeded event. セルContextMenuStripプロパティの設定は、行をオーバーライドContextMenuStripプロパティの設定とCellContextMenuStripNeededイベント両方の上書き、RowContextMenuStripNeededイベントと、行ContextMenuStripプロパティの設定。The cell ContextMenuStrip property setting overrides the row ContextMenuStrip property setting, and the CellContextMenuStripNeeded event overrides both the RowContextMenuStripNeeded event and the row ContextMenuStrip property setting. 指定できるnullただし、セルのショートカット メニューの行のショートカット メニューがオーバーライドされないようにします。You can specify null for a cell shortcut menu, however, to prevent a row shortcut menu from being overridden.

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。For more information about how to handle events, see Handling and Raising Events.

コンストラクター

DataGridViewCellContextMenuStripNeededEventArgs(Int32, Int32) DataGridViewCellContextMenuStripNeededEventArgs(Int32, Int32) DataGridViewCellContextMenuStripNeededEventArgs(Int32, Int32) DataGridViewCellContextMenuStripNeededEventArgs(Int32, Int32)

DataGridViewCellContextMenuStripNeededEventArgs クラスの新しいインスタンスを初期化します。Initializes a new instance of the DataGridViewCellContextMenuStripNeededEventArgs class.

プロパティ

ColumnIndex ColumnIndex ColumnIndex ColumnIndex

イベントが発生したセルの列インデックスを示す値を取得します。Gets a value indicating the column index of the cell that the event occurs for.

(Inherited from DataGridViewCellEventArgs)
ContextMenuStrip ContextMenuStrip ContextMenuStrip ContextMenuStrip

CellContextMenuStripNeeded イベントの発生元であるセルのショートカット メニューを取得または設定します。Gets or sets the shortcut menu for the cell that raised the CellContextMenuStripNeeded event.

RowIndex RowIndex RowIndex RowIndex

イベントが発生したセルの行インデックスを示す値を取得します。Gets a value indicating the row index of the cell that the event occurs for.

(Inherited from DataGridViewCellEventArgs)

メソッド

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象

こちらもご覧ください