When cell is selected I am changing its color, but I need that when user clicks/select a cell it color get change and if user clicks another cell (in same row), both the new cell and the previous cell gets selected and color changes of both cells, AND if user click the first cell again then it gets de-selected and only the second cell is selected with changed color.
Here is my code:
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;
}
}
Right now when I select some cell it color changes but when I move to some other cell and select it, then previously selected cell gets deselected and its changed color revert back to default color.
Aucun commentaire:
Enregistrer un commentaire