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;
}
}