DataGridViewCellContextMenuStripNeededEventArgs 类

定义

CellContextMenuStripNeeded 事件提供数据。

C#
public class DataGridViewCellContextMenuStripNeededEventArgs : System.Windows.Forms.DataGridViewCellEventArgs
继承
DataGridViewCellContextMenuStripNeededEventArgs

示例

下面的代码示例使用 DataGridViewCellContextMenuStripNeededEventArgs 类设置快捷菜单,而不取消共享行。

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

注解

CellContextMenuStripNeeded仅当设置了控件DataSource属性或其 VirtualMode 属性为 trueDataGridView,才会发生该事件。

处理 CellContextMenuStripNeeded 事件时,每当用户右键单击单元格时,都将显示处理程序中指定的快捷菜单。 如果要显示由单元格的当前状态或值确定的快捷菜单,这非常有用。

每当 CellContextMenuStripNeeded 以编程方式检索属性的值 DataGridViewCell.ContextMenuStrip 或用户右键单击单元格时,也会发生该事件。

可以使用 ColumnIndexRowIndex 属性来确定单元格的状态或值,并使用此信息设置属性 ContextMenuStrip 。 此属性使用事件值替代的单元格 ContextMenuStrip 属性的值进行初始化。

CellContextMenuStripNeeded处理大量数据时处理 事件,以避免设置多个单元格的单元格ContextMenuStrip值对性能造成损失。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法

还可以通过设置行属性或处理DataGridView控件的 RowContextMenuStripNeeded 事件,为单个行ContextMenuStrip而不是单个单元格指定快捷菜单。 单元格 ContextMenuStrip 属性设置将替代行 ContextMenuStrip 属性设置,而 CellContextMenuStripNeeded 事件将同时 RowContextMenuStripNeeded 覆盖 事件和行 ContextMenuStrip 属性设置。 但是,可以为单元格快捷菜单指定 null ,以防止覆盖行快捷菜单。

有关如何处理事件的详细信息,请参阅 处理和引发事件

构造函数

属性

ColumnIndex

获取一个值,此值指示发生此事件的单元格的列索引。

(继承自 DataGridViewCellEventArgs)
ContextMenuStrip

获取或设置引发 CellContextMenuStripNeeded 事件的单元格的快捷菜单。

RowIndex

获取一个值,此值指示发生此事件的单元格的行索引。

(继承自 DataGridViewCellEventArgs)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅