0
votes

enter image description hereenter image description hereMy requirement : In datagridview I need select row by clicking the row header and selected row should maintain until I will click the other row header also same time I should select cell too.

My Problem : I can't select multiple row using Shift and Ctrl key.

my code :

    List< DataGridViewRow> selectedRows = new List< DataGridViewRow>();

    void selectRows()
    {
        dataGridView1.SuspendLayout();
        foreach (DataGridViewRow r in dataGridView1.Rows)
        {
            r.Selected = selectedRows.Contains(r);
        }
        dataGridView1.ResumeLayout();
    }

    private void dataGridView1_RowHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
    {
        DataGridViewRow clickedRow = dataGridView1.CurrentRow;

        if (selectedRows.Contains(clickedRow))
        {
            selectedRows.Remove(clickedRow);
        }
        else
        {
            selectedRows.Add(clickedRow);
        }
        selectRows();

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if ((row.Index != e.RowIndex) && !row.Selected)
            {
                row.DefaultCellStyle.BackColor = Color.White;
            }
            else
            {
                selectedRows.Remove(clickedRow);
                row.Selected = true;
                row.DefaultCellStyle.BackColor = Color.Blue;
            }
        }
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.DefaultCellStyle.BackColor == Color.Blue)
            {
                row.Selected = true;
            }
        }
    }
2
Shift and Ctrl should work with DataGridView by defult. Did you change any of the properties?sora0419
No i didn't change anything..V.V
You will need to provide a little bit more info here. We do not have the info to help you figure out what the problem is.sora0419
I want to select multiple row also maintain the selected row and when i select any cell also should highlight... here all are working fine but only issue is i cant select multiple row.. if i select row use ctrl or shift key, the row automatically deselect..V.V
So, while pressing Ctrl, you want to be able to select multiple rows when clicking row header, and select multiple cells when clicking on cell? So by the end you can have multple rows and multiple cells selected, while the selected cell could be on a different row than the selected row? If this is true, can you please provide the properties of your DataGridView? Because with the default setting in Visual Studio 2010, it should work automatically.sora0419

2 Answers

0
votes

You have to set your enable your datagridview's multiselect dataGridView.MultiSelect = true;

0
votes

Be familiar with using a debugger, you will be able to find out where the issue is.

You are clearing the selection in your loop

foreach (DataGridViewRow row in dataGridView1.Rows)
{
}

Rethink the if-else logic in it and you will see why. You are clearing your previous selections when you are not suppose to.