2010-06-14 60 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

    相关问题