DataGridView的右键菜单(ContextMenuStrip)转载
原创I.属性的使用I.属性的使用I.使用属性
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 财产。这可以通过设置 ContextMenuStrip 对象控制对象来控制 DataGridView 将显示右击菜单的。将显示右键单击菜单。 DataGridViewColumn 的 ContextMenuStrip 属性已设置,属性已设置为 列标题以外的单元格的右击菜单。 DataGridViewRow 的 ContextMenuStrip 属性已设置,属性已设置为除了行头以外的单元格的右键菜单。DataGridViewCell 的 ContextMenuStrip 属性已设置,属性已设置为指定单元格的右键菜单。
[C#]
1
2
3
4
5
6
7
8
9
10
// DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip =
this
.ContextMenuStrip1;
// 列的 ContextMenuStrip 设定
DataGridView1.Columns[0].ContextMenuStrip =
this
.ContextMenuStrip2;
// 列头的 ContextMenuStrip 设定
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip =
this
.ContextMenuStrip2;
// 行的 ContextMenuStrip 设定
DataGridView1.Rows[0].ContextMenuStrip =
this
.ContextMenuStrip3;
// 单元格的 ContextMenuStrip 设定
DataGridView1[0, 0].ContextMenuStrip =
this
.ContextMenuStrip4;
对于单元格上的右键菜单设置,优先顺序为 Cell > Row > Column > DataGridView
二、使用dataGridView的 CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设置单元格的右击菜单,尤其是在需要根据单元格的值更改右击菜单的情况下 (例如,您需要确定(例如,您需要确定(例如,您需要确定cell的值大于10时使用ContextMenuStrip1,小于10时使用ContextMenuStrip2 然后你需要使用,那么它就必须使用CellContextMenuStripNeeded(要设置的事件)(要设置的事件)(事件已设置) 。使用此事件设置右击菜单比使用循环遍历更有效。然而,DataGridView使用了DataSourceBINDING和YES BINDING AND是BIND和YES BIND AND ISVirtualMode当事件不被触发时,该事件将不被触发。当事件不会被引发时。
[C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// CellContextMenuStripNeeded事件处理方法事件处理方法事件处理方法事件处理方法
private
void
DataGridView1_CellContextMenuStripNeeded(
object
sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if
(e.RowIndex < 0)
{
// 列头的ContextMenuStrip设定
e.ContextMenuStrip =
this
.ContextMenuStrip1;
}
else
if
(e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip =
this
.ContextMenuStrip2;
}
else
if
(dgv[e.ColumnIndex, e.RowIndex].Value
is
int
)
{
// 如果像元值是整数如果像元值是整数如果像元值是整数
e.ContextMenuStrip =
this
.ContextMenuStrip3;
}
}
同样,也可以通过以下方式获取相同的信息 RowContextMenuStripNeeded 事件来设置该行的右击菜单。事件来设置该行的右击菜单。
[C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
// RowContextMenuStripNeeded事件处理方法事件处理方法事件处理方法事件处理方法
private
void
DataGridView1_RowContextMenuStripNeeded(
object
sender,
DataGridViewRowContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
// 当"Column1"列是Bool型且为True时间,设置它的时间,设置它的时间,设置它们的时候设置它的ContextMenuStrip
object
boolVal = dgv[
"Column1"
, e.RowIndex].Value;
Console.WriteLine(boolVal);
if
(boolVal
is
bool
&& (
bool
)boolVal)
{
e.ContextMenuStrip =
this
.ContextMenuStrip1;
}
}
CellContextMenuStripNeeded 事件处理方法事件处理方法事件处理方法在事件处理方法的参数中,e.ColumnIndex=-1表示行,“”表示行,“e.RowIndex=-1“指示列标题。”表示列标题。“表示列标题。RowContextMenuStripNeeded然后就不会有不会就不会有e.RowIndex=-1情况。“The Situation of”的情况
-分界线
1以上只是一个设置,上面只是一个设置ContextMenuStrip方法时,右击不会更改当前行号,即:当前行号为1但右键单击,但右键单击第一个2行,弹出ContextMenuStrip菜单,此时当前行仍然是第一行1行,currentRow所获得的数据也是第一次1数据行,那么如何在更改当前行号的同时单击鼠标右键呢?
可以使用CellMouseDown该活动解决了这一问题。
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
//contextMenuStrip_ListViewItemRightClick.Show(MousePosition.X, MousePosition.Y);
}
}
}
2如果不需要根据单元格的值设置不同的值ContextMenuStrip然后使用RowTemplate的ContextMenuStrip属性设置ContextMenuStrip足够了。这就够了。这就足够了。这将是足够的。
——————————————————再分割——————————————————————————————
对于treeview这个解决方案也存在类似的问题。
//对于treeview可以应用mousedown事务
办法一:
1
2
3
4
5
6
7
8
9
10
11
private
void
treeView1_MouseDown(
object
sender, MouseEventArgs e)
{
if
(e.Button == MouseButtons.Right)
{
TreeNode node =
this
.treeView1.GetNodeAt(e.Location);
if
(node !=
null
)
{
this
.treeView1.SelectedNode = node;
}
}
}
办法二:
1
2
3
4
5
6
7
8
9
10
void
jcsTreeView1_MouseDown(
object
sender, MouseEventArgs e)
{
System.Windows.Forms.TreeViewHitTestInfo hittestinfo =
this
.jcsTreeView1.HitTest(e.X ,e.Y);
if
(hittestinfo.Node !=
null
)
{
TreeViewHitTestLocations loc = hittestinfo.Location;
if
(loc == TreeViewHitTestLocations.Label )
MessageBox.Show(hittestinfo.Node.Text);
}
}
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除