Skip to content

DGV Row Selection and Deletion #291

Description

@itg-assistant

Cris asked 2016-09-28 18:04:59 UTC

Hi,

After selecting multiple rows in datagridview and mouse right-click in a row other that the last row selected to view the context menu, SelectedRows.Count is reset to zero (0) as the table-row-indicator moves to the row where I initiated the mouse right-click. Rows selected remain highlighted though. Code below fails and nothing is deleted. Must right-click  on the last row select. In WinForm, the table-row-indicator doesn't change location after right-click

If I select non-contiguous rows, unselected row in deleted and selected row remains undeleted. Please see attached images.

In both instances, I am using this code:

if (dgv.SelectedRows.Count > 0)
{
    foreach (DataGridViewRow row in dgv.SelectedRows)
    {
        if (!row.IsNewRow)
        {
            dgv.Rows.Remove(row);
        }
    }
}

Am I missing something?

And please confirm. If a row is new (IsNewRow=true), RowHeaderMouseClick doesn't flag the row as selected and
SelectedRows.Count remains zero (0). Is that by design?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions