简体   繁体   English

删除datagridview中的多行

[英]Delete multiple rows in datagridview

I have a function to delete single rows on right click delete in a datagridview..我有一个功能可以在 datagridview 中右键单击删除删除单行..

code:代码:

  private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {

            if (e.Button == MouseButtons.Right)
            {
                var hti = dataGridView1.HitTest(e.X, e.Y);
                if (hti.RowIndex != -1)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hti.RowIndex].Selected = true;
                }
            }          
    }

    private void DeleteRow_Click(object sender, EventArgs e)
    {
            Int32 rowToDelete = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);
            if (rowToDelete != -1)
            {
                dataGridView1.Rows.RemoveAt(rowToDelete);
                dataGridView1.ClearSelection();
            }          
    }

but now I want to delete multiple rows on selection.但现在我想在选择时删除多行。
First I don't know why I cannot select multiple rows.首先我不知道为什么我不能选择多行。
Second I want to delete multiple delete using the delete button and right click mouse delete.其次,我想使用删除按钮并右键单击鼠标删除来删除多个删除。

Can someone help me?有人能帮我吗?

Edit: Take a look at your code.编辑:看看你的代码。 You are setting the selected row depending on the results of the HitTest method.您正在根据HitTest方法的结果设置所选行。 The DataGridView property SelectedRows will determine which rows are selected. DataGridView属性SelectedRows将确定选择了哪些行。 Not sure why you need to execute a HitTest , but then again perhaps you haven't fully explained the desired functionality.不确定为什么需要执行HitTest ,但也许您还没有完全解释所需的功能。

if (e.Button == MouseButtons.Right)
{
    var hti = dataGridView1.HitTest(e.X, e.Y);
    if (hti.RowIndex != -1)
    {
        dataGridView1.ClearSelection();
        dataGridView1.Rows[hti.RowIndex].Selected = true;
    }
}

Make sure that the MultiSelect property is set to true on your datagrid.确保数据网格上的MultiSelect属性设置为true

Then, you can utilize the SelectedRows property in the event of your choice:然后,您可以在您选择的情况下使用SelectedRows属性:

foreach (DataGridViewRow row in DataGridView1.SelectedRows)
{
    DataGridView1.Rows.Remove(row);
}

please take care the following case:请注意以下情况:

if you need to delete records in datagrid, don't just store the rowIndex in datagrid, (instead you should store the corresponding keys in DB):如果需要删除datagrid中的记录,不要只将rowIndex存储在datagrid中,(而是应该将相应的键存储在DB中):

eg: I want to delete row 1 and 2 in datagrid, I stored their rowIndex in datagrid.例如:我想删除数据网格中的第 1 行和第 2 行,我将它们的 rowIndex 存储在数据网格中。 after row 1 is deleted in datagrid, data in row 2 will SHIFT UP to row 1, and data in row 3 will SHIFT UP to row 2, because you are using the datagrid rowIndex to locate what data to delete, therefore, result: data1 and data3 will be deleted finally.在datagrid中删除第1行后,第2行的数据会上移到第1行,第3行的数据会上移到第2行,因为你是用datagrid的rowIndex来定位要删除的数据,所以,结果:data1 data3 最终会被删除。

            foreach (DataGridViewRow row in dgShow.SelectedRows)
            {
                string id = row.Cells[4].Value.ToString();
                int x = Convert.ToInt16(id);
                var del = context.ServicesPrice.Where(current => current.Id == x).FirstOrDefault();
                context.ServicesPrice.Remove(del);
            }

you can write instead of cells[4]: columns id in your DB.你可以写而不是 cell[4]: columns id 在你的数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM