DataGridView中使用ContextMenuStrip實現右鍵菜單

 

 

ContextMenuStrip

鼠標右鍵 
1、添加控件,將ContextMenuStrip拖到視圖設計器的下方或在工具箱裏雙擊ContextMenuStrip添加 

 

此時在左下角會顯示ContextMenuStrip,點擊之後

新增一個【新增,修改,刪除】的右鍵菜單

 

然後雙擊新增的【新增,修改,刪除】菜單,進入到點擊事件中,編寫提示框代碼。

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("新增成功");
        }


 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            BllOpDatabaseManager.dbName = User.aliTmpDBName;
            //獲得當前選中的行 
            //int rowindex = e.RowIndex;

            string strid = Convert.ToString(dataGridView1.CurrentCell.RowIndex);
            string strUVPC = dataGridView1.Rows[rowindex].Cells["商品訪客數"].Value.ToString();
            string strUV = dataGridView1.Rows[rowindex].Cells["訪客數"].Value.ToString();

            //string cmdstr = "update Users set UserName='" + txtUname.Text + "',UserPass='" + txtUpass.Text + "' where UserID=" + Convert.ToInt32(strid) + "";
            //string cmdstr = "update 店鋪整體日指標 set 商品訪客數=" + strUVPC + ",訪客數=" + strUV + " where 統計日期=" + dataGridView1.Rows[rowindex].Cells[0].Value.ToString() + "";
            string cmdstr = "update 店鋪整體日指標 set 商品訪客數=" + Convert.ToInt32(strUVPC) + ",訪客數=" + Convert.ToInt32(strUV) + " where 統計日期='" + dataGridView1.Rows[rowindex].Cells[0].Value.ToString() + "'";
            MessageBox.Show(cmdstr, "友情提示");
            IsExecuteNon = BllOpDatabaseManager.ExecuteNonQuery(cmdstr);
            if (IsExecuteNon)
            {
                //MessageBox.Show("記錄已從數據庫刪除,請按刷新按鈕刷新顯示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Display();  //顯示數據庫內容
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失敗!");
            }

       
        }



 private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            bool IsExecuteNon = false;
            BllOpDatabaseManager.dbName = User.aliTmpDBName;
            //獲得當前選中的行 
            //int rowindex = e.RowIndex;
            //string cmdstr = string.Format("delete from 店鋪整體日指標 where 統計日期 = '{0}'", dataGridView1.SelectedRows[0].Cells[0].Value);

            string cmdstr = string.Format("delete from 店鋪整體日指標 where 統計日期 = '{0}'", dataGridView1.Rows[rowindex].Cells[0].Value.ToString() );
            MessageBox.Show(cmdstr, "友情提示");
            IsExecuteNon = BllOpDatabaseManager.DelBtn_Click(cmdstr);
            if (IsExecuteNon)
            {
                //MessageBox.Show("記錄已從數據庫刪除,請按刷新按鈕刷新顯示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Display();  //顯示數據庫內容
                MessageBox.Show("刪除成功!");
            }
            else
            {
                MessageBox.Show("刪除失敗!");
            }
   
        }

        private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //dataGridviewView中刪除功能就實現
            dataGridView1.AllowUserToAddRows = false;//刪除最後一行的空白行
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//刪除當前光標所在行 
            MessageBox.Show("移除成功!");
            //dataGridView1.Rows.Clear();//刪除所有行
        }


        //更改當前單元格時,返回數據庫,事件
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            BllOpDatabaseManager.dbName = User.aliTmpDBName;

            int myint = dataGridView1.CurrentCell.ColumnIndex;//當前列索引
            int myint2 = dataGridView1.CurrentCell.RowIndex;//當前行索引
            string str1 = dataGridView1.Columns[myint].HeaderText + "=" + "'" + dataGridView1.CurrentCell.Value.ToString() + "'";//列名+當前格內容
            string str2 = dataGridView1.Rows[myint2].Cells[0].Value.ToString();//當前行,0列內容。
            string cmdstr = "update 店鋪整體日指標 set " + str1 + " where 統計日期 ='" + str2 + "'";//sql更改語句


            MessageBox.Show(cmdstr, "友情提示");
            IsExecuteNon = BllOpDatabaseManager.ExecuteNonQuery(cmdstr);
            if (IsExecuteNon)
            {
                //MessageBox.Show("記錄已從數據庫刪除,請按刷新按鈕刷新顯示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Display();  //顯示數據庫內容
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失敗!");
            }

        }











dataGridviewView中刪除功能就實現

        private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            dataGridView1.AllowUserToAddRows = false;//刪除最後一行的空白行
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//刪除當前光標所在行 
            MessageBox.Show("刪除成功!");
            //dataGridView1.Rows.Clear();//刪除所有行

        }

 

建立關聯

找到DataGridView的屬性中的ContextMenuStrip,將其設置爲上面拖拽的右鍵菜單控件。

 

 

 

 

 

 

 

發佈了101 篇原創文章 · 獲贊 41 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章