DataGridViewCellContextMenuStripNeededEventArgsとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataGridViewCellContextMenuStripNeededEventArgsの意味・解説 

DataGridViewCellContextMenuStripNeededEventArgs クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

CellContextMenuStripNeeded イベントデータ提供します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Class DataGridViewCellContextMenuStripNeededEventArgs
    Inherits DataGridViewCellEventArgs
Dim instance As DataGridViewCellContextMenuStripNeededEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs
 : DataGridViewCellEventArgs
public ref class DataGridViewCellContextMenuStripNeededEventArgs
 : public DataGridViewCellEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs
 extends DataGridViewCellEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs
 extends DataGridViewCellEventArgs
解説解説

CellContextMenuStripNeeded イベント発生するのは、DataGridView コントロールの DataSource プロパティ設定されているか、VirtualMode プロパティtrue場合のみです。

CellContextMenuStripNeeded イベント処理すると、ユーザーセル右クリックするたびに、ハンドラ指定したショートカット メニュー表示されます。これは、セル現在の状態または値に応じたショートカット メニュー表示する場合に便利です。

CellContextMenuStripNeeded イベントは、プログラムによって、またはユーザーセル右クリックしたときに、DataGridViewCell.ContextMenuStrip プロパティの値が取得されたときにも発生します

ColumnIndex プロパティと RowIndex プロパティ使用すると、セルの状態または値を特定でき、さらにこの情報使用して、ContextMenuStrip プロパティ設定できます。このプロパティは、イベントの値によってオーバーライドされるセルContextMenuStrip プロパティの値を使用して初期化されます

大量データ処理するときは、CellContextMenuStripNeeded イベント処理して複数セルに対してセルContextMenuStrip 値を設定することによるパフォーマンスの低下防いでください詳細については、「Windows フォーム DataGridView コントロール拡張するための推奨される手順」を参照してください

行の ContextMenuStrip プロパティ設定するか、DataGridView コントロールの RowContextMenuStripNeeded イベント処理して個別セルではなく個別の行にショートカット メニュー指定することもできますセルContextMenuStrip プロパティ設定は、行の ContextMenuStrip プロパティ設定オーバーライドし、CellContextMenuStripNeeded イベントは、RowContextMenuStripNeeded イベントと行の ContextMenuStrip プロパティ設定両方オーバーライドます。ただし、セルショートカット メニューnull 参照 (Visual Basic では Nothing) を指定すると、行のショートカット メニューオーバーライドされるの回避できます

イベント処理詳細については、「イベント利用」を参照してください

使用例使用例

DataGridViewCellContextMenuStripNeededEventArgs クラス使用して、行の共有解除せずにショートカット メニュー設定するコード例次に示します

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
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();
}
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();
}
継承階層継承階層
System.Object
   System.EventArgs
     System.Windows.Forms.DataGridViewCellEventArgs
      System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventArgs
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridViewCellContextMenuStripNeededEventArgs メンバ
System.Windows.Forms 名前空間
DataGridView クラス
DataGridView.CellContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnCellContextMenuStripNeeded
DataGridViewCellContextMenuStripNeededEventHandler
DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewCell.ContextMenuStrip プロパティ
DataGridView.RowContextMenuStripNeeded イベント
DataGridViewRow.ContextMenuStrip
ContextMenuStrip クラス
その他の技術情報
Windows フォーム DataGridView コントロール拡張するための推奨される手順

DataGridViewCellContextMenuStripNeededEventArgs コンストラクタ

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

DataGridViewCellContextMenuStripNeededEventArgs クラス新しインスタンス初期化します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Sub New ( _
    columnIndex As Integer, _
    rowIndex As Integer _
)
Dim columnIndex As Integer
Dim rowIndex As Integer

Dim instance As New DataGridViewCellContextMenuStripNeededEventArgs(columnIndex,
 rowIndex)
public DataGridViewCellContextMenuStripNeededEventArgs (
    int columnIndex,
    int rowIndex
)
public:
DataGridViewCellContextMenuStripNeededEventArgs (
    int columnIndex, 
    int rowIndex
)
public DataGridViewCellContextMenuStripNeededEventArgs (
    int columnIndex, 
    int rowIndex
)
public function DataGridViewCellContextMenuStripNeededEventArgs
 (
    columnIndex : int, 
    rowIndex : int
)

パラメータ

columnIndex

イベント発生したセルの列インデックス

rowIndex

イベント発生したセルの行インデックス

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridViewCellContextMenuStripNeededEventArgs クラス
DataGridViewCellContextMenuStripNeededEventArgs メンバ
System.Windows.Forms 名前空間
DataGridView クラス
DataGridView.CellContextMenuStripNeeded イベント
DataGridView.OnCellContextMenuStripNeeded

DataGridViewCellContextMenuStripNeededEventArgs プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ ColumnIndex  イベント発生したセルの列インデックスを示す値を取得します。 ( DataGridViewCellEventArgs から継承されます。)
パブリック プロパティ ContextMenuStrip CellContextMenuStripNeeded イベントの発生元であるセルショートカット メニュー取得または設定します
パブリック プロパティ RowIndex  イベント発生したセルの行インデックスを示す値を取得します。 ( DataGridViewCellEventArgs から継承されます。)
参照参照

関連項目

DataGridViewCellContextMenuStripNeededEventArgs クラス
System.Windows.Forms 名前空間
DataGridView クラス
DataGridView.CellContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnCellContextMenuStripNeeded
DataGridViewCellContextMenuStripNeededEventHandler
DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewCell.ContextMenuStrip プロパティ
DataGridView.RowContextMenuStripNeeded イベント
DataGridViewRow.ContextMenuStrip
ContextMenuStrip クラス

その他の技術情報

Windows フォーム DataGridView コントロール拡張するための推奨される手順

DataGridViewCellContextMenuStripNeededEventArgs メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataGridViewCellContextMenuStripNeededEventArgs クラス
System.Windows.Forms 名前空間
DataGridView クラス
DataGridView.CellContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnCellContextMenuStripNeeded
DataGridViewCellContextMenuStripNeededEventHandler
DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewCell.ContextMenuStrip プロパティ
DataGridView.RowContextMenuStripNeeded イベント
DataGridViewRow.ContextMenuStrip
ContextMenuStrip クラス

その他の技術情報

Windows フォーム DataGridView コントロール拡張するための推奨される手順

DataGridViewCellContextMenuStripNeededEventArgs メンバ

CellContextMenuStripNeeded イベントデータ提供します

DataGridViewCellContextMenuStripNeededEventArgs データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DataGridViewCellContextMenuStripNeededEventArgs DataGridViewCellContextMenuStripNeededEventArgs クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ ColumnIndex  イベント発生したセルの列インデックスを示す値を取得します。(DataGridViewCellEventArgs から継承されます。)
パブリック プロパティ ContextMenuStrip CellContextMenuStripNeeded イベントの発生元であるセルショートカット メニュー取得または設定します
パブリック プロパティ RowIndex  イベント発生したセルの行インデックスを示す値を取得します。(DataGridViewCellEventArgs から継承されます。)
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataGridViewCellContextMenuStripNeededEventArgs クラス
System.Windows.Forms 名前空間
DataGridView クラス
DataGridView.CellContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnCellContextMenuStripNeeded
DataGridViewCellContextMenuStripNeededEventHandler
DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewCell.ContextMenuStrip プロパティ
DataGridView.RowContextMenuStripNeeded イベント
DataGridViewRow.ContextMenuStrip
ContextMenuStrip クラス

その他の技術情報

Windows フォーム DataGridView コントロール拡張するための推奨される手順



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataGridViewCellContextMenuStripNeededEventArgs」の関連用語

DataGridViewCellContextMenuStripNeededEventArgsのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGridViewCellContextMenuStripNeededEventArgsのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS