国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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ó)家法律
紫阳县| 安吉县| 望都县| 泾川县| 富民县| 松潘县| 安阳市| 安岳县| 资兴市| 松阳县| 务川| 长宁县| 东丽区| 临江市| 万年县| 威宁| 夏河县| 合阳县| 铅山县| 永福县| 通河县| 黎平县| 芜湖市| 项城市| 徐水县| 固原市| 江川县| 万安县| 江口县| 武安市| 凤翔县| 馆陶县| 彭水| 筠连县| 灵川县| 南乐县| 钦州市| 盐津县| 西乌| 谷城县| 和平县|