locked
How to delete selected row in a DataGridView? RRS feed

  • Question

  • How can I delete selected row in a DataGridView, if possible without loops.
    • Edited by MrigaShira Tuesday, June 19, 2012 7:26 PM
    Tuesday, June 19, 2012 7:24 PM

Answers

  • Hello MrigaShira,

    How can I delete selected row in a DataGridView, if possible without loops.

    yes , is possible with RemoveAt() method.

    dataGridView1.Rows.RemoveAt("your int index");

    Regards.


    Tuesday, June 19, 2012 7:29 PM
  • Hello MrigaShira,

    Carmelo, SelectedRowIndex can vary.

    is true, whether you run the event RowHeaderMouseClick select the row to be deleted and the steps to the method RemoveAt () using the property e.RowIndex.

            private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                dataGridView1.Rows.RemoveAt(e.RowIndex);
            }

    Regards.

    Tuesday, June 19, 2012 8:17 PM
  • Hello MrigaShira,

    CurrentRow is not allways SelectedRow.

    ok, you can enhance it with the variable an integer, as long as the present DataGrd, me for example I used e.RowIndex, but no one stops you from writing 10, 12 etc., as long as I repeat the number you enter is actually a line that exists datagrid in order to avoid exceptions index out range exception at runtime.

    Regards.


    Tuesday, June 19, 2012 9:44 PM

All replies

  • Hello MrigaShira,

    How can I delete selected row in a DataGridView, if possible without loops.

    yes , is possible with RemoveAt() method.

    dataGridView1.Rows.RemoveAt("your int index");

    Regards.


    Tuesday, June 19, 2012 7:29 PM
  • Carmelo, SelectedRowIndex can vary.

    Tuesday, June 19, 2012 8:12 PM
  • Hello MrigaShira,

    Carmelo, SelectedRowIndex can vary.

    is true, whether you run the event RowHeaderMouseClick select the row to be deleted and the steps to the method RemoveAt () using the property e.RowIndex.

            private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                dataGridView1.Rows.RemoveAt(e.RowIndex);
            }

    Regards.

    Tuesday, June 19, 2012 8:17 PM
  • buttonClick:

    dgv.SelectedRows.Delete

    something like that.


    • Edited by MrigaShira Tuesday, June 19, 2012 8:49 PM
    • Proposed as answer by Jason Dot Wang Wednesday, June 20, 2012 9:28 AM
    • Unproposed as answer by Jason Dot Wang Wednesday, June 27, 2012 7:16 AM
    Tuesday, June 19, 2012 8:48 PM
  • Hello MrigaShira,

    buttonClick:

    dgv.SelectedRows.Delete

    something like that.

    the only ways to remove a row from a datagrid are two, the Remove method and RemoveAt that I recommend :), you can manage it the way I wrote in my last pot or this.

     private void button1_Click(object sender, EventArgs e)
            {
                var selectedRow = dgv.CurrentRow.Index;
                dgv.Rows.RemoveAt(selectedRow);
            }

    Regards

    Tuesday, June 19, 2012 8:56 PM
  • CurrentRow is not allways SelectedRow.
    Tuesday, June 19, 2012 9:39 PM
  • Hello MrigaShira,

    CurrentRow is not allways SelectedRow.

    ok, you can enhance it with the variable an integer, as long as the present DataGrd, me for example I used e.RowIndex, but no one stops you from writing 10, 12 etc., as long as I repeat the number you enter is actually a line that exists datagrid in order to avoid exceptions index out range exception at runtime.

    Regards.


    Tuesday, June 19, 2012 9:44 PM