|
1.CellContextMenuStripNeeded 事件仅在设置了 DataGridView 控件的 DataSource 属性或者该控件的 VirtualMode 属性为 true 时发生。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 事件时,每当用户右击单元格时,就会显示您在处理程序中指定的快捷菜单。如果您希望显示由单元格的当前状态或当前值确定的快捷菜单,此事件非常有用。
每当检索 DataGridViewCell.ContextMenuStrip 属性的值时(无论是通过编程方式,还是当用户右击单元格时),CellContextMenuStripNeeded 事件也会发生。
可以使用 DataGridViewCellEventArgs.ColumnIndex 和 RowIndex 属性来确定单元格的状态或值,并使用此信息来更改或修改 DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip 属性。此属性是使用单元格的 ContextMenuStrip 属性值初始化的,该值会被事件值重写。
在处理大量数据时应处理 CellContextMenuStripNeeded 事件,这样,在为多个单元格设置单元格的 ContextMenuStrip 值时,可以避免性能受到影响。有关更多信息,请参见 缩放 Windows 窗体 DataGridView 控件的最佳做法。
还可以通过设置行的 ContextMenuStrip 属性或处理 RowContextMenuStripNeeded 事件来为各个行(而非各个单元格)指定快捷菜单。单元格的 ContextMenuStrip 属性设置重写行的 ContextMenuStrip 属性设置,CellContextMenuStripNeeded 事件既重写 RowContextMenuStripNeeded 事件,又重写行的 ContextMenuStrip 属性设置。但是,可以通过为单元格快捷菜单指定 空引用(在 Visual Basic 中为 Nothing)来阻止重写行快捷菜单。
2.RowContextMenuStripNeeded 事件仅在设置了 DataGridView 控件的 DataSource 属性或者该控件的 VirtualMode 属性为 true 时发生。当您希望显示由行的当前状态或它所包含的值确定的快捷菜单时,处理 RowContextMenuStripNeeded 事件非常有用。
当处理 RowContextMenuStripNeeded 事件时,除非 CellContextMenuStripNeeded 重写被单击的特定单元格的快捷菜单,否则,当用户右击某一行时,将显示您在处理程序中指定的快捷菜单。
只要检索 DataGridViewRow..::.ContextMenuStrip 属性的值,无论是通过编程方式,还是用户右击某行,RowContextMenuStripNeeded 事件也都会发生。
可以使用 DataGridViewRowContextMenuStripNeededEventArgs..::.RowIndex 属性来确定行的状态或行所包含的值,然后使用此信息来更改或修改 DataGridViewRowContextMenuStripNeededEventArgs..::.ContextMenuStrip 属性。此属性是使用行的 ContextMenuStrip 属性值初始化的,该值会被事件值重写。
在处理大量数据时应处理 RowContextMenuStripNeeded 事件,这样,在为多行设置行的 ContextMenuStrip 值时,可以避免性能受到影响。有关更多信息,请参见缩放 Windows 窗体 DataGridView 控件的最佳做法。
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;
} (责任编辑:admin) |