0
votes

I have a Gridview with 30 rows and 12 Columns with multiselect true and Selection Mode : Cell Select. I want to do following thing:

a) When a user selects cells, color of cells should change. However I want to limit user, so that it can select multiple cells from single row only at a time i.e user can not select multiple cells across different rows.

b) If user select multiple cells in a row then it color changes and cell remain selected until user re-select it to change to remove color change (de-select to default color). Right now if user selects certain cells it color changes but when user move to select other cells, then previously selected cells are changing back to default color (de-selects automatically).

Right now I am able to change color of selected cells but I am not able to meet above conditions.

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewColumn col in dataGridView1.Columns)
                {

                   dataGridView1.Rows[row.Index].DefaultCellStyle.SelectionBackColor = Color.Pink;

                }
            }

[Update]: I have tried this but still not able to solve my problem

    private void dataGridView1_CellClick(object sender,
       DataGridViewCellEventArgs e)
    {
        List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();



        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        {
            rowCollection.Add(dataGridView1.Rows[cell.RowIndex]);

        }




        foreach (DataGridViewRow row in rowCollection)
        {

                dataGridView1.Rows[row.Index].DefaultCellStyle.SelectionBackColor = Color.Pink;


        }

    }
2

2 Answers

0
votes

I recommend looking into the default events for DataGridViews. From the sounds of your conditions, you'll need a list of selected cells (or perhaps there's already a flag on the Cell object you can evaluate), and you'll want to add an event handler for the Cell Click event. So you can check the row of a cell in your 'selectedCells' list against the new cell being clicked. If they're the same you can colour that cell. You could also check for the existence of that cell within the list already, indicating that you'd want to remove the colouring.

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick%28v=vs.110%29.aspx

Hope this helps! :)

0
votes

a) You can do it simply with the DataGridView's SelectionChanged event :

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    int currentRowIndex = dataGridView1.CurrentCell.RowIndex;

    foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        if (cell.RowIndex != currentRowIndex)
            cell.Selected = false;
}

b) Your problem is not clear for me. It looks like the same than a). By the way, you can select multiple cells in DataGridView by holding the CTRL key.

Finally, don't use DefaultCellStyle.SelectionBackColor, it is quite slow. I would suggest you to handle the DataGridView's CellPainting event to color cells :

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if ((e.State & DataGridViewElementStates.Selected) != 0)
    {
        e.CellStyle.SelectionBackColor = Color.Pink;
    }
}