最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

C# DataGridView內(nèi)自定義右鍵菜單

2023-03-04 01:31 作者:FFFFFourier  | 我要投稿

1. 在工具欄內(nèi)找到ContextMenuStrip,拖拽到目標(biāo)頁(yè)面內(nèi)


工具欄 ContextMenuStrip

2. 在頁(yè)面內(nèi)添加ContextMenuStrip包含的項(xiàng)目


向ContextMenuStrip內(nèi)添加項(xiàng)目

3. 添加DataGridView的事件

我自己的需求是,右鍵單擊表格內(nèi)特定區(qū)域的單元格,在相應(yīng)的位置彈出右鍵菜單列表。但是,DataGridView默認(rèn)只支持鼠標(biāo)左鍵單鍵選擇行或選擇單元格,所以需要在DataGridView的事件函數(shù)里自定義鼠標(biāo)的行為。其次,最好不要在DataGridView的屬性里面直接綁定ContextMenuStrip,如下圖

最好不要在DataGridView的屬性里面綁定ContextMenuStrip

因?yàn)橐坏┰谶@里綁定,那么任何情況下只要鼠標(biāo)在DataGridView區(qū)域內(nèi)右鍵單擊,都會(huì)彈出菜單!(如果期望的效果本來(lái)就是這樣的話,忽略上述內(nèi)容)

為了實(shí)現(xiàn)在指定單元格右鍵時(shí)彈出菜單,需要自己定義事件函數(shù),直接貼代碼。

private void dgv_logs_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

? ? ? ? {

? ? ? ? ? ? if (e.Button == MouseButtons.Right && e.RowIndex >= 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? dgv_logs.ClearSelection();

? ? ? ? ? ? ? ? dgv_logs.CurrentCell = null;

? ? ? ? ? ? ? ? dgv_logs.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;

? ? ? ? ? ? ? ? if (e.ColumnIndex > 4)

? ? ? ? ? ? ? ? ? ? dgv_logs.ContextMenuStrip = contextMenuStrip_logs;

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? dgv_logs.ContextMenuStrip = null;

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? private void dgv_logs_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)

? ? ? ? {

? ? ? ? ? ? if (e.Button == MouseButtons.Right && e.RowIndex >= 0 && e.ColumnIndex > 4)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? contextMenuStrip_logs.Show(MousePosition.X, MousePosition.Y);

? ? ? ? ? ? ? ? dgv_logs.ContextMenuStrip = null;

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? private void ToolStripMenuItem_logs_openInExplore_Click(object sender, EventArgs e)

? ? ? ? {

? ? ? ? ? ? int RowIndex = dgv_logs.CurrentCell.RowIndex;

? ? ? ? ? ? int ColIndex = dgv_logs.CurrentCell.ColumnIndex;

? ? ? ? ? ? MessageBox.Show(RowIndex.ToString() + ColIndex.ToString());

? ? ? ? }

4. 實(shí)現(xiàn)效果

右鍵單擊指定區(qū)域單元格
MessageBox提示該單元格的行列號(hào)


C# DataGridView內(nèi)自定義右鍵菜單的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
垫江县| 德阳市| 尼勒克县| 金寨县| 湟中县| 商丘市| 游戏| 连江县| 潍坊市| 马鞍山市| 漳州市| 祁阳县| 聂荣县| 汝城县| 丰台区| 西安市| 汶川县| 淄博市| 威海市| 罗平县| 土默特右旗| 石台县| 南宫市| 金溪县| 工布江达县| 浦东新区| 大英县| 嫩江县| 肥乡县| 霍林郭勒市| 仁化县| 红河县| 利辛县| 吕梁市| 宜昌市| 蓝田县| 施秉县| 浠水县| 孟村| 弋阳县| 九台市|