C# 實(shí)現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼
在窗體中添加DataGridView控件和ConTextMenuStrip1控件,修改DataGridView屬性,將contextMenuStrip控件綁定dataGridView控件

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //選中整行可在屬性中修改 datagridview.AutoGenerateColumns = false;//不讓datagridview自動生成列,可在屬性中修改 datagridview.AllowUserToAddRows = true;//禁止自動生成行可在屬性中修改
代碼實(shí)現(xiàn)選中一行,右鍵出現(xiàn)菜單
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
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];
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
catch (Exception)
{
MessageBox.Show("請選擇內(nèi)容");
}

總結(jié)
到此這篇關(guān)于C# 實(shí)現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的文章就介紹到這了,更多相關(guān)C# dataGridView選中一行右鍵出現(xiàn)菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
學(xué)完設(shè)計(jì)模式之后,你就感覺它會慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式的相關(guān)資料,需要的朋友可以參考下2021-08-08
WPF利用RichTextBox實(shí)現(xiàn)富文本編輯器
在實(shí)際應(yīng)用中,富文本隨處可見,那么在WPF開發(fā)中,如何實(shí)現(xiàn)富文本編輯呢?本文以一個(gè)簡單的小例子,簡述如何通過RichTextBox實(shí)現(xiàn)富文本編輯功能,需要的可以參考下2024-02-02

