DataGridViewRowContextMenuStripNeededEventArgs クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


RowContextMenuStripNeeded イベントは、DataGridView コントロールの DataSource プロパティが設定されているか、VirtualMode プロパティが true の場合にだけ発生します。RowContextMenuStripNeeded イベントを処理すると、行の現在の状態や行に含まれる値によって決定されるショートカット メニューを表示する場合に便利です。
RowContextMenuStripNeeded イベントを処理すると、ユーザーが行を右クリックするたびに、ハンドラで指定したショートカット メニューが表示されます。ただし、クリックされた特定のセルのショートカット メニューが、CellContextMenuStripNeeded イベントによってオーバーライドされている場合を除きます。
RowContextMenuStripNeeded イベントは、プログラム上で、またはユーザーが行を右クリックしたときに、DataGridViewRow.ContextMenuStrip プロパティの値が取得されたときにも発生します。
RowIndex プロパティを使用すると、行の状態や行に含まれる値を特定し、その情報を使用して ContextMenuStrip プロパティを変更できます。このプロパティは、行の ContextMenuStrip プロパティの値によって初期化され、イベントの値によってオーバーライドされます。
RowContextMenuStripNeeded イベントは、多くのデータを処理するときに、複数行の ContextMenuStrip の値を設定することによるパフォーマンスの低下を防ぐために使用します。詳細については、「Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。

RowContextMenuStripNeeded イベントを処理して、従業員の役職に応じた ContextMenuStrip を表示するコード例を次に示します。この例では、2 種類のショートカット メニューがあり、1 つは経営者向け、もう 1 つはその他のすべての従業員向けです。
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Drawing Imports System.Windows.Forms Class Form1 Inherits Form Private WithEvents dataGridView1 As New DataGridView() Private WithEvents employeeMenuStrip As New ContextMenuStrip() Private WithEvents managerMenuStrip As New ContextMenuStrip() Private WithEvents toolStripMenuItem1 As New ToolStripMenuItem() Private WithEvents toolStripMenuItem2 As New ToolStripMenuItem() Private WithEvents toolStripMenuItem3 As New ToolStripMenuItem() Private contextMenuRowIndex As Integer Public Sub New() Me.Size = New Size(700, 300) Me.Controls.Add(dataGridView1) toolStripMenuItem1.Text = "View Employee Sales Report" toolStripMenuItem2.Text = "View Team Sales Report" toolStripMenuItem3.Text = "View Company Sales Team Ranking Report" With employeeMenuStrip .Items.Add(toolStripMenuItem1) End With With managerMenuStrip .Items.Add(toolStripMenuItem2) .Items.Add(toolStripMenuItem3) End With PopulateDataGridView() End Sub ' Establish the main entry point for the application. <STAThreadAttribute()> _ Public Shared Sub Main() Application.Run(New Form1()) End Sub Private Sub PopulateDataGridView() With dataGridView1 .Dock = DockStyle.Fill .AllowUserToAddRows = False .MultiSelect = False .ReadOnly = True .AllowUserToDeleteRows = False End With Dim query As String query = "SELECT e1.*, e2.FirstName + ' ' + e2.LastName AS Manager " query &= "FROM employees AS e1 LEFT JOIN employees AS e2 " query &= "ON e1.ReportsTo = e2.EmployeeID" ' Connect to the database Dim sqlConnection1 As New SqlConnection( _ "Integrated Security=SSPI;Persist Security Info=False;" & _ "Initial Catalog=Northwind;Data Source=localhost") Dim sqlDataAdapter1 As New SqlDataAdapter(query, _ sqlConnection1) Dim dataTable1 As New System.Data.DataTable() dataTable1.Locale = System.Globalization.CultureInfo.InvariantCulture sqlDataAdapter1.Fill(dataTable1) dataGridView1.DataSource = dataTable1 End Sub Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, _ ByVal e As DataGridViewBindingCompleteEventArgs) _ Handles dataGridView1.DataBindingComplete ' Hide some of the columns. With dataGridView1 .Columns("EmployeeID").Visible = False .Columns("Address").Visible = False .Columns("TitleOfCourtesy").Visible = False .Columns("BirthDate").Visible = False .Columns("HireDate").Visible = False .Columns("PostalCode").Visible = False .Columns("Photo").Visible = False .Columns("Notes").Visible = False .Columns("ReportsTo").Visible = False .Columns("PhotoPath").Visible = False End With ' Disable sorting for the DataGridView. Dim i As DataGridViewColumn For Each i In dataGridView1.Columns i.SortMode = DataGridViewColumnSortMode.NotSortable Next i dataGridView1.AutoResizeColumns() End Sub Public Sub dataGridView1_RowContextMenuStripNeeded( _ ByVal sender As Object, _ ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _ Handles dataGridView1.RowContextMenuStripNeeded Dim dataGridViewRow1 As DataGridViewRow = _ dataGridView1.Rows(e.RowIndex) toolStripMenuItem1.Enabled = True ' Show the appropriate ContextMenuStrip based on the employees title. If dataGridViewRow1.Cells("Title").Value.ToString() = _ "Sales Manager" OrElse _ dataGridViewRow1.Cells("Title").Value.ToString() = _ "Vice President, Sales" Then e.ContextMenuStrip = managerMenuStrip Else e.ContextMenuStrip = employeeMenuStrip End If contextMenuRowIndex = e.RowIndex End Sub Public Sub dataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _ Handles dataGridView1.CellToolTipTextNeeded Dim newLine As String = Environment.NewLine If e.RowIndex > -1 Then Dim dataGridViewRow1 As DataGridViewRow = _ dataGridView1.Rows(e.RowIndex) ' Add the employee's ID to the ToolTipText. e.ToolTipText = String.Format("EmployeeID {0}: {1}", _ dataGridViewRow1.Cells("EmployeeID").Value.ToString(), _ newLine) ' Add the employee's name to the ToolTipText. e.ToolTipText += String.Format("{0} {1} {2} {3}", _ dataGridViewRow1.Cells("TitleOfCourtesy").Value.ToString(), _ dataGridViewRow1.Cells("FirstName").Value.ToString(), _ dataGridViewRow1.Cells("LastName").Value.ToString(), _ newLine) ' Add the employee's title to the ToolTipText. e.ToolTipText += String.Format("{0}{1}{2}", _ dataGridViewRow1.Cells("Title").Value.ToString(), _ newLine, newLine) ' Add the employee's contact information to the ToolTipText. e.ToolTipText += String.Format("{0}{1}{2}, ", _ dataGridViewRow1.Cells("Address").Value.ToString(), newLine, _ dataGridViewRow1.Cells("City").Value.ToString()) If Not String.IsNullOrEmpty( _ dataGridViewRow1.Cells("Region").Value.ToString()) e.ToolTipText += String.Format("{0}, ", _ dataGridViewRow1.Cells("Region").Value.ToString()) End If e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}", _ dataGridViewRow1.Cells("Country").Value.ToString(), _ dataGridViewRow1.Cells("PostalCode").Value.ToString(), _ newLine, _ dataGridViewRow1.Cells("HomePhone").Value.ToString(), _ dataGridViewRow1.Cells("Extension").Value.ToString(), _ newLine, newLine) ' Add employee information to the ToolTipText. Dim HireDate As DateTime = _ CType(dataGridViewRow1.Cells("HireDate").Value, DateTime) e.ToolTipText += _ String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}", _ HireDate.Month.ToString(), HireDate.Day.ToString(), _ HireDate.Year.ToString(), newLine, _ dataGridViewRow1.Cells("Manager").Value.ToString()) End If End Sub Public Sub toolStripMenuItem1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles toolStripMenuItem1.Click Dim dataGridViewRow1 As DataGridViewRow = _ dataGridView1.Rows(contextMenuRowIndex) MessageBox.Show(String.Format( _ "Sales Report for {0} {1}:{2}{3}Reporting Not Implemented", _ dataGridViewRow1.Cells("FirstName").Value.ToString(), _ dataGridViewRow1.Cells("LastName").Value.ToString(), _ Environment.NewLine, Environment.NewLine)) End Sub Public Sub toolStripMenuItem2_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles toolStripMenuItem2.Click Dim dataGridViewRow1 As DataGridViewRow = _ dataGridView1.Rows(contextMenuRowIndex) MessageBox.Show(String.Format( _ "Sales Report for {0} {1}:{2}{3}Reporting Not Implemented", _ dataGridViewRow1.Cells("FirstName").Value.ToString(), _ dataGridViewRow1.Cells("LastName").Value.ToString(), _ Environment.NewLine, Environment.NewLine)) End Sub Public Sub toolStripMenuItem3_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles toolStripMenuItem3.Click Dim dataGridViewRow1 As DataGridViewRow = _ dataGridView1.Rows(contextMenuRowIndex) MessageBox.Show(String.Format( _ "Company Sales Ranking Report:{0}{1}Reporting not implemented.", _ Environment.NewLine, Environment.NewLine)) End Sub End Class
#region Using directives using System; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Windows.Forms; #endregion class Form1 : Form { private DataGridView dataGridView1 = new DataGridView(); private ContextMenuStrip employeeMenuStrip = new ContextMenuStrip(); private ContextMenuStrip managerMenuStrip = new ContextMenuStrip(); private ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem(); private ToolStripMenuItem toolStripMenuItem2 = new ToolStripMenuItem(); private ToolStripMenuItem toolStripMenuItem3 = new ToolStripMenuItem(); private int contextMenuRowIndex; public Form1() { // Initialize the form. // This code can be replaced with designer generated code. this.Size = new Size(700, 300); this.Controls.Add(dataGridView1); toolStripMenuItem1.Text = "View Employee Sales Report"; toolStripMenuItem2.Text = "View Team Sales Report"; toolStripMenuItem3.Text = "View Company Sales Team Ranking Report"; } protected override void OnLoad(EventArgs e) { dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler( dataGridView1_DataBindingComplete); dataGridView1.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler( dataGridView1_CellToolTipTextNeeded); dataGridView1.RowContextMenuStripNeeded += new DataGridViewRowContextMenuStripNeededEventHandler( dataGridView1_RowContextMenuStripNeeded); toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click); toolStripMenuItem2.Click += new EventHandler(toolStripMenuItem2_Click); toolStripMenuItem3.Click += new EventHandler(toolStripMenuItem3_Click); employeeMenuStrip.Items.Add(toolStripMenuItem1); managerMenuStrip.Items.Add(toolStripMenuItem2); managerMenuStrip.Items.Add(toolStripMenuItem3); PopulateDataGridView(); base.OnLoad(e); } // Establish the main entry point for the application. [STAThreadAttribute()] public static void Main() { Application.Run(new Form1()); } // Replace this with your own code to populate the DataGridView. private void PopulateDataGridView() { dataGridView1.Dock = DockStyle.Fill; dataGridView1.AllowUserToAddRows = false; dataGridView1.MultiSelect = false; dataGridView1.ReadOnly = true; dataGridView1.AllowUserToDeleteRows = false; string query; query = "SELECT e1.*, e2.FirstName + ' ' + e2.LastName AS Manager " + "FROM employees AS e1 LEFT JOIN employees AS e2 " + "ON e1.ReportsTo = e2.EmployeeID"; // Connect to the database SqlConnection sqlConnection1 = new SqlConnection( "Integrated Security=SSPI;Persist Security Info=False;" + "Initial Catalog=Northwind;Data Source=localhost"); SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(query , sqlConnection1); DataTable dataTable1 = new DataTable(); dataTable1.Locale = System.Globalization.CultureInfo.InvariantCulture; sqlDataAdapter1.Fill(dataTable1); dataGridView1.DataSource = dataTable1; } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { // Hide some of the columns. dataGridView1.Columns["EmployeeID"].Visible = false; dataGridView1.Columns["Address"].Visible = false; dataGridView1.Columns["TitleOfCourtesy"].Visible = false; dataGridView1.Columns["BirthDate"].Visible = false; dataGridView1.Columns["HireDate"].Visible = false; dataGridView1.Columns["PostalCode"].Visible = false; dataGridView1.Columns["Photo"].Visible = false; dataGridView1.Columns["Notes"].Visible = false; dataGridView1.Columns["ReportsTo"].Visible = false; dataGridView1.Columns["PhotoPath"].Visible = false; // Disable sorting for the DataGridView. foreach (DataGridViewColumn i in dataGridView1.Columns) { i.SortMode = DataGridViewColumnSortMode.NotSortable; } dataGridView1.AutoResizeColumns(); } void dataGridView1_RowContextMenuStripNeeded(object sender , DataGridViewRowContextMenuStripNeededEventArgs e) { DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex]; toolStripMenuItem1.Enabled = true; // Show the appropriate ContextMenuStrip based on the employees title. if ((dataGridViewRow1.Cells["Title"].Value.ToString() == "Sales Manager") || (dataGridViewRow1.Cells["Title"].Value.ToString() == "Vice President, Sales")) { e.ContextMenuStrip = managerMenuStrip; } else { e.ContextMenuStrip = employeeMenuStrip; } contextMenuRowIndex = e.RowIndex; } void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { string newLine = Environment.NewLine; if (e.RowIndex > -1) { DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex]; // Add the employee's ID to the ToolTipText. e.ToolTipText = String.Format("EmployeeID {0}:{1}", dataGridViewRow1.Cells["EmployeeID"].Value, newLine); // Add the employee's name to the ToolTipText. e.ToolTipText += String.Format("{0} {1} {2}{3}", dataGridViewRow1.Cells["TitleOfCourtesy"].Value.ToString() , dataGridViewRow1.Cells["FirstName"].Value.ToString(), dataGridViewRow1.Cells["LastName"].Value.ToString(), newLine); // Add the employee's title to the ToolTipText. e.ToolTipText += String.Format("{0}{1}{2}", dataGridViewRow1.Cells["Title"].Value.ToString(), newLine, newLine); // Add the employee's contact information to the ToolTipText. e.ToolTipText += String.Format("{0}{1}{2}, ", dataGridViewRow1.Cells["Address"].Value.ToString(), newLine , dataGridViewRow1.Cells["City"].Value.ToString()); if (!String.IsNullOrEmpty( dataGridViewRow1.Cells["Region"].Value.ToString())) { e.ToolTipText += String.Format("{0}, ", dataGridViewRow1.Cells["Region"].Value.ToString()); } e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}" , dataGridViewRow1.Cells["Country"].Value.ToString(), dataGridViewRow1.Cells["PostalCode"].Value.ToString(), newLine, dataGridViewRow1.Cells["HomePhone"].Value.ToString() , dataGridViewRow1.Cells["Extension"].Value.ToString(), newLine, newLine); // Add employee information to the ToolTipText. DateTime HireDate = (DateTime)dataGridViewRow1.Cells["HireDate"].Value; e.ToolTipText += String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}" , HireDate.Month.ToString(), HireDate.Day.ToString(), HireDate.Year.ToString(), newLine, dataGridViewRow1.Cells["Manager"].Value.ToString()); } } void toolStripMenuItem1_Click(object sender, EventArgs e) { DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[contextMenuRowIndex]; MessageBox.Show(String.Format( "Sales Report for {0} {1}{2}{3}Reporting not implemented.", dataGridViewRow1.Cells["FirstName"].Value.ToString(), dataGridViewRow1.Cells["LastName"].Value.ToString(), Environment.NewLine, Environment.NewLine)); } void toolStripMenuItem2_Click(object sender, EventArgs e) { DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[contextMenuRowIndex]; MessageBox.Show(String.Format( "Sales Report for {0} {1}'s Team{2}{3}Reporting not implemented.", dataGridViewRow1.Cells["FirstName"].Value.ToString(), dataGridViewRow1.Cells["LastName"].Value.ToString(), Environment.NewLine, Environment.NewLine)); } void toolStripMenuItem3_Click(object sender, EventArgs e) { DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[contextMenuRowIndex]; MessageBox.Show(String.Format( "Company Sales Ranking Report:{0}{1}Reporting not implemented." , Environment.NewLine, Environment.NewLine)); } }

System.EventArgs
System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventArgs


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridViewRowContextMenuStripNeededEventArgs メンバ
System.Windows.Forms 名前空間
ContextMenuStrip
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnRowContextMenuStripNeeded
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRow.ContextMenuStrip プロパティ
DataGridViewRowContextMenuStripNeededEventArgs コンストラクタ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim rowIndex As Integer Dim instance As New DataGridViewRowContextMenuStripNeededEventArgs(rowIndex)
- rowIndex
行のインデックス。



Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridViewRowContextMenuStripNeededEventArgs プロパティ

名前 | 説明 | |
---|---|---|
![]() | ContextMenuStrip | RowContextMenuStripNeeded イベントの発生元である行のショートカット メニューを取得または設定します。 |
![]() | RowIndex | ショートカット メニューを要求している行のインデックスを取得します。 |

関連項目
DataGridViewRowContextMenuStripNeededEventArgs クラスSystem.Windows.Forms 名前空間
ContextMenuStrip
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnRowContextMenuStripNeeded
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRow.ContextMenuStrip プロパティ
DataGridViewRowContextMenuStripNeededEventArgs メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
DataGridViewRowContextMenuStripNeededEventArgs クラスSystem.Windows.Forms 名前空間
ContextMenuStrip
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnRowContextMenuStripNeeded
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRow.ContextMenuStrip プロパティ
DataGridViewRowContextMenuStripNeededEventArgs メンバ
RowContextMenuStripNeeded イベントのデータを提供します。
DataGridViewRowContextMenuStripNeededEventArgs データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | DataGridViewRowContextMenuStripNeededEventArgs | DataGridViewRowContextMenuStripNeededEventArgs クラスの新しいインスタンスを初期化します。 |

名前 | 説明 | |
---|---|---|
![]() | ContextMenuStrip | RowContextMenuStripNeeded イベントの発生元である行のショートカット メニューを取得または設定します。 |
![]() | RowIndex | ショートカット メニューを要求している行のインデックスを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
DataGridViewRowContextMenuStripNeededEventArgs クラスSystem.Windows.Forms 名前空間
ContextMenuStrip
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridView.OnRowContextMenuStripNeeded
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRow.ContextMenuStrip プロパティ
- DataGridViewRowContextMenuStripNeededEventArgsのページへのリンク