5

I have a function to delete single rows on right click delete in a 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?

3 Answers 3

10

Edit: Take a look at your code. You are setting the selected row depending on the results of the HitTest method. The DataGridView property SelectedRows will determine which rows are selected. Not sure why you need to execute a HitTest, but then again perhaps you haven't fully explained the desired functionality.

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.

Then, you can utilize the SelectedRows property in the event of your choice:

foreach (DataGridViewRow row in DataGridView1.SelectedRows)
{
    DataGridView1.Rows.Remove(row);
}
Sign up to request clarification or add additional context in comments.

4 Comments

the multiple select works.. but on right click delete... when i select multiple rows and right click it selects only one row... why is this happening
Have you checked the SelectionMode property of the datagrid? Are you sure you are selecting the full row and not just a cell?
yes i am selecting the full row and not just a cell..where is the selection mode..?? thanks
my selection mode is row header select
3

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):

eg: I want to delete row 1 and 2 in datagrid, I stored their rowIndex in datagrid. 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.

Comments

0
            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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.