在Form上拖放baiContextMenuStrip並添加一個“刪除”菜單項,並添加此菜du單項的Click事件處理代碼,如下zhi所示,設置DataGridView的ContextMenuStrip屬性dao爲新加入的ContextMenuStrip。
private void toolStripMenuItem_Click(object sender, EventArgs e) { DataGridView dv = sender as DataGridView; if (dv != null && dv.CurrentCell!=null && dv.CurrentCell.RowIndex!=-1) { dv.Rows.RemoveAt(dv.CurrentCell.RowIndex); } }
切記一定要設置DataGridView的ContextMenuStrip屬性dao爲新加入的ContextMenuStrip。