Zac
Zac

Reputation: 2373

CTRL-A selects hidden rows in DataGridView

I've got a DataGridView object in a C# program of mine that, depending on which filter settings are turned on, sometimes has hidden rows in it. I have the MultiSelect setting in the DGV set to True and when another button is clicked, the ID from each row is moved over to a ListBox. However, I've noticed if I hit CTRL-A that all the rows, including the ones that are hidden, are moved over as well. I'm hiding the rows by changing their Visible property to False.

I looked around but couldn't find a solution to have the CTRL-A command only select visible rows. Is this possible?

Thanks!

Upvotes: 0

Views: 1803

Answers (1)

Yuriy Guts
Yuriy Guts

Reputation: 2220

You can implement this custom behavior by handling the grid's KeyDown event. In your case, you can write something like this:

private void grid_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
    {
        foreach (DataGridViewRow row in grid.Rows)
        {
            row.Selected = row.Visible;
        }
        e.Handled = true;
        e.SuppressKeyPress = true;
    }
}

Upvotes: 5

Related Questions