2010-06-14 122 views
48

我在DataGridView中有幾列,並且在我的行中有數據。我在這裏看到了幾個解決方案,但我無法將它們合併!用鼠標右鍵在Datagridview中選擇一行並顯示一個刪除它的菜單

只需右鍵單擊一行,它將選擇整行並顯示一個菜單,其中包含一個用於刪除行的選項,選擇該選項時將刪除該行。

我做了很少的嘗試,但沒有工作,它看起來很混亂。我該怎麼辦?

+0

你的問題太模糊。在遇到問題時添加更多詳細信息。你想做什麼,並不是很困難。 – leppie 2010-06-14 06:11:47

回答

84

我終於解決了這個問題:

  • 在Visual Studio中創建一個的ContextMenuStrip一個名爲 「DeleteRow」

    在DataGridView的鏈接的ContextMenuStrip

  • 然後使用一個項目下面的代碼幫助我得到它的工作。

    this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); 
    this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click); 
    

    這裏是最酷的部分

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    { 
        if(e.Button == MouseButtons.Right) 
        { 
         var hti = MyDataGridView.HitTest(e.X, e.Y); 
         MyDataGridView.ClearSelection(); 
         MyDataGridView.Rows[hti.RowIndex].Selected = true; 
        } 
    } 
    
    private void DeleteRow_Click(object sender, EventArgs e) 
    { 
        Int32 rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected); 
        MyDataGridView.Rows.RemoveAt(rowToDelete); 
        MyDataGridView.ClearSelection(); 
    } 
    

    我希望代碼可以幫助別人:-)

    我歡迎任何修正,如果有一個錯誤。

  • +0

    你在DeleteRow_Click中使用了什麼控件? – Crimsonland 2011-02-22 23:40:38

    +0

    是的,它是點擊 – 2011-04-28 07:57:31

    +0

    當網格上出現'ContextMenuStripNeeded'事件時,這個回答似乎不起作用。使用'CellMouseDown'工作。 – 2013-04-25 14:59:41

    1
    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    { 
        if(e.Button == MouseButtons.Right) 
        { 
         MyDataGridView.ClearSelection(); 
         MyDataGridView.Rows[e.RowIndex].Selected = true; 
        } 
    } 
    
    private void DeleteRow_Click(object sender, EventArgs e) 
    { 
        Int32 rowToDelete = MyrDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected); 
        MyDataGridView.Rows.RemoveAt(rowToDelete); 
        MyDataGridView.ClearSelection(); 
    } 
    
    3

    它更容易爲鼠標按下只添加事件:

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    { 
        if (e.Button == MouseButtons.Right) 
        { 
         var hti = MyDataGridView.HitTest(e.X, e.Y); 
         MyDataGridView.Rows[hti.RowIndex].Selected = true; 
         MyDataGridView.Rows.RemoveAt(rowToDelete); 
         MyDataGridView.ClearSelection(); 
        } 
    } 
    

    這種情況很容易。你必須通過以下方式啓動你的mousedown事件:

    this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); 
    

    在你的構造函數中。

    28

    爲了完善這個問題,最好使用Grid事件而不是鼠標。

    首先設置你的DataGrid的屬性:

    的SelectionMode到FullRowSelect 和 RowTemplate /的ContextMenuStrip於上下文菜單。

    創建CellMouseDown事件: -

    private void myDatagridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 
        if (e.Button == MouseButtons.Right) 
        { 
         int rowSelected = e.RowIndex; 
         if (e.RowIndex != -1) 
         { 
          this.myDatagridView.Rows[rowSelected].Selected = true; 
         } 
         // you now have the selected row with the context menu showing for the user to delete etc. 
        } 
    } 
    
    +0

    當「ContextMenuStripNeeded」存在時,此答案有效。基於'MouseDown'的解決方案沒有。 – 2013-04-25 14:58:15

    +0

    @peter如果選中多行,如何獲取行的索引? – Vbp 2013-12-22 19:53:00

    +0

    嗨vbp,我認爲你需要提出一個新的問題,如果還沒有回答,這個問題是指只選擇1行。 – peterincumbria 2013-12-29 10:48:17

    0

    您也可以使用該稍微簡單的事件裏面的代碼如下:

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    {  
        if (e.Button == MouseButtons.Right)  
        {   
         rowToDelete = e.RowIndex; 
         MyDataGridView.Rows.RemoveAt(rowToDelete);   
         MyDataGridView.ClearSelection();  
        } 
    } 
    
    +0

    這隻會刪除沒有警告或確認的行。我確定OP不希望那樣。 – ProfK 2012-09-28 08:18:46

    0

    看到這裏可以使用DataGridView完成RowTemplate財產。

    注意:此代碼未經測試,但我以前使用過此方法。

    // Create DataGridView 
    DataGridView gridView = new DataGridView(); 
    gridView.AutoGenerateColumns = false; 
    gridView.Columns.Add("Col", "Col"); 
    
    // Create ContextMenu and set event 
    ContextMenuStrip cMenu = new ContextMenuStrip(); 
    ToolStripItem mItem = cMenu.Items.Add("Delete"); 
    mItem.Click += (o, e) => { /* Do Something */ };   
    
    // This makes all rows added to the datagridview use the same context menu 
    DataGridViewRow defaultRow = new DataGridViewRow(); 
    defaultRow.ContextMenuStrip = cMenu; 
    

    然後你去,就這麼簡單!

    2

    對此問題提出的所有答案均基於鼠標單擊事件。您還可以將ContenxtMenuStrip分配給您的DataGridview,並檢查當用戶RightMouseButtonsDataGridView上時是否選擇了一行,並決定是否要查看ContenxtMenuStrip。您可以通過設置的ContextMenuStrip

    private void MyContextMenuStrip_Opening(object sender, CancelEventArgs e) 
        { 
         //Only show ContextMenuStrip when there is 1 row selected. 
         if (MyDataGridView.SelectedRows.Count != 1) e.Cancel = true; 
        } 
    

    的的Opening eventCancelEventArgs.Cancel值這樣做,但如果你有幾個上下文菜單條,用含有不同的選擇,根據不同的選擇,我會去一個鼠標器點擊進入自己以及。

    6
    private void dgvOferty_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) 
        { 
         dgvOferty.ClearSelection(); 
         int rowSelected = e.RowIndex; 
         if (e.RowIndex != -1) 
         { 
          this.dgvOferty.Rows[rowSelected].Selected = true; 
         } 
         e.ContextMenuStrip = cmstrip; 
        } 
    

    塔達:D。最簡單的方式。對於自定義單元格只需修改一下。

    +1

    最好的方法,也適用於僅使用鍵盤。但是需要警告:只有在連接了DataSource的情況下才有效。有關DataGridView.CellContextMenuStripNeeded事件的MSDN:「只有在設置了DataGridView控件的DataSource屬性或其VirtualMode屬性爲true時,纔會發生CellContextMenuStripNeeded事件。」 – 2016-02-12 08:27:30

    +0

    什麼是'cmstrip'變量引用? – reformed 2016-04-06 20:50:38

    1

    上@資料庫答案基地將無法正常工作,直到進行選擇模式FullRow

    MyDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
    

    但如果你需要使它在CellSelect模式下工作

    MyDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect; 
    
    // for cell selection 
    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    { 
        if(e.Button == MouseButtons.Right) 
        { 
         var hit = MyDataGridView.HitTest(e.X, e.Y); 
         MyDataGridView.ClearSelection(); 
    
         // cell selection 
         MyDataGridView[hit.ColumnIndex,hit.RowIndex].Selected = true; 
        } 
    } 
    
    private void DeleteRow_Click(object sender, EventArgs e) 
    { 
        int rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected); 
        MyDataGridView.Rows.RemoveAt(rowToDelete); 
        MyDataGridView.ClearSelection(); 
    } 
    
    0

    我有一個新的解決方法以相同的結果,但代碼少。 爲的WinForms ...這就是例子是葡萄牙語 跟進步步

    1. 在窗體創建的ContextMenuStrip並創建一個項目
    2. 註冊一個事件點擊(OnCancelarItem_Click)此的ContextMenuStrip enter image description here
    3. 在GridView控件
    4. 創建一個事件「UserDeletingRow」 enter image description here 現在......你已經從模擬使用者在按鍵德爾

      你不會忘記在gridview上啓用刪除,對不對?

    enter image description here enter image description here 終於... enter image description here

    相關問題