五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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ó)家法律
新野县| 区。| 贵德县| 禄丰县| 巫溪县| 凉城县| 临夏县| 固阳县| 周口市| 洪湖市| 柘城县| 兴化市| 昭苏县| 神木县| 纳雍县| 玉林市| 扶绥县| 长汀县| 阳山县| 嵊州市| 中方县| 蓬莱市| 汝州市| 许昌县| 巴林左旗| 宝鸡市| 澄城县| 龙川县| 沙湾县| 阳江市| 开远市| 盖州市| 县级市| 原阳县| 裕民县| 阳曲县| 蒙自县| 尼木县| 政和县| 孝义市| 铜鼓县|