locked
Deleting a selected row in datagrid view RRS feed

  • Question

  • I want to delete a selected row in datagrid view. in Visual C++, can anyone say the code for that ?
    Tuesday, August 2, 2011 8:13 AM

Answers

  • Hello sreejith.ck,

    Is the DataGridView data binded?

    First option: The user can delete rows by first selecting the rows and then pressing the Delete key(on the condition that you set the DataGridView's SelectionMode to FullRowSelect, and setting AllowUserToDeleteRows to true).

    Second, if using code, then

    dataGridView1.Rows.Remove(DataGridViewRow) or dataGridView1.Rows.RemoveAt(int),
    The following code snippet removes all the selected rows in the DataGridView control:
     
    foreach (DataGridViewRow row in DataGridView1.SelectedRows)
    {
        DataGridView1.Rows.Remove(row);
    }
    you can find sample code on http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

    You can modify values or remove rows regardless of whether the control is bound to an external data source. If there is a data source, the changes are made directly to the data source. You may still need to push the data source updates to a remote database, however. For more information, see How to: Bind Data to the Windows Forms DataGridView Control.

    You may find more and more information about delete rows in DataGridView by googling "delete row dataGridView", that will make you understand this topic more practically.

    Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Proposed as answer by Carmelo La Monica Friday, August 5, 2011 10:49 AM
    • Marked as answer by Helen Zhou Monday, August 15, 2011 9:31 AM
    Friday, August 5, 2011 8:31 AM

All replies

  • Hello sreejith.ck,

    Is the DataGridView data binded?

    First option: The user can delete rows by first selecting the rows and then pressing the Delete key(on the condition that you set the DataGridView's SelectionMode to FullRowSelect, and setting AllowUserToDeleteRows to true).

    Second, if using code, then

    dataGridView1.Rows.Remove(DataGridViewRow) or dataGridView1.Rows.RemoveAt(int),
    The following code snippet removes all the selected rows in the DataGridView control:
     
    foreach (DataGridViewRow row in DataGridView1.SelectedRows)
    {
        DataGridView1.Rows.Remove(row);
    }
    you can find sample code on http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

    You can modify values or remove rows regardless of whether the control is bound to an external data source. If there is a data source, the changes are made directly to the data source. You may still need to push the data source updates to a remote database, however. For more information, see How to: Bind Data to the Windows Forms DataGridView Control.

    You may find more and more information about delete rows in DataGridView by googling "delete row dataGridView", that will make you understand this topic more practically.

    Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Proposed as answer by Carmelo La Monica Friday, August 5, 2011 10:49 AM
    • Marked as answer by Helen Zhou Monday, August 15, 2011 9:31 AM
    Friday, August 5, 2011 8:31 AM
  • Also "this->dataGridView1->RemoveAt(index)" can be useful.
    Saturday, March 30, 2019 5:35 AM