当我选择一个单元格时,我希望它几乎不易察觉(就像在 excel 中一样)。但是通过我的实现,出于某种原因,您可以看到单元格是如何按顺序绘制的,如何以一种快速的方式修复代码?多亏了while循环,我自己才稍微加快了速度。
我确定有更简单的方法
void MyForm::dataGridView1_SelectionChanged(Object^ sender, EventArgs^ e)
{
//Выделение активной ячейки по столбцу и строке
CurrColNew = dataGridView1->CurrentCell->ColumnIndex;
CurrRowNew = dataGridView1->CurrentCell->RowIndex;
short col = CurrColNew;
short row = CurrRowNew;
short colold = CurrColOld;
short rowold = CurrRowOld;
Color& rowheadbackcolornew = dataGridView1->Columns[CurrColNew]->HeaderCell->Style->BackColor;
int stp = 65;
if (CurrColOld != CurrColNew || CurrRowOld != CurrRowNew)
{
if (rowheadbackcolornew == Color::FromArgb(127, 118, 121))
rowheadbackcolornew = Color::FromArgb(127, 118 + stp, 121);
else if (rowheadbackcolornew == Color::FromArgb(0, 190, 20))
rowheadbackcolornew = Color::FromArgb(0, 190 + stp, 20);
else if (rowheadbackcolornew == Color::FromArgb(0, 162, 254))
rowheadbackcolornew = Color::FromArgb(0, 162 + stp, 254);
else if (rowheadbackcolornew == Color::FromArgb(190, 0, 0))
rowheadbackcolornew = Color::FromArgb(190, 0 + stp, 0);
else if (rowheadbackcolornew == Color::FromArgb(255, 255, 255))
rowheadbackcolornew = Color::FromArgb(255, 245, 194);
dataGridView1->Columns[CurrColNew]->HeaderCell->Style->BackColor = rowheadbackcolornew;
short i = 0;
while (i < colold)
{
if (getColorCell(i, rowold) == Color::FromArgb(255, 245, 194))
SetColor(i, rowold, Color::FromArgb(255, 255, 255));
i++;
}
short j = 0;
while (j < rowold)
{
if (getColorCell(colold, j) == Color::FromArgb(255, 245, 194))
SetColor(colold, j, Color::FromArgb(255, 255, 255));
j++;
}
short _i = 0;
while (_i < col)
{
if (getColorCell(_i, row) == Color::FromArgb(255, 255, 255))
SetColor(_i, row, Color::FromArgb(255, 245, 194));
_i++;
}
short _j = 0;
while (_j < row)
{
if (getColorCell(col, _j) == Color::FromArgb(255, 255, 255))
SetColor(col, _j, Color::FromArgb(255, 245, 194));
_j++;
}
}
Color& rowheadbackcolorold = dataGridView1->Columns[CurrColOld]->HeaderCell->Style->BackColor;
if (rowheadbackcolorold == Color::FromArgb(127, 118 + stp, 121))
rowheadbackcolorold = Color::FromArgb(127, 118, 121);
else if (rowheadbackcolorold == Color::FromArgb(0, 190 + stp, 20))
rowheadbackcolorold = Color::FromArgb(0, 190, 20);
else if (rowheadbackcolorold == Color::FromArgb(0, 162 + stp, 254))
rowheadbackcolorold = Color::FromArgb(0, 162, 254);
else if (rowheadbackcolorold == Color::FromArgb(190, 0 + stp, 0))
rowheadbackcolorold = Color::FromArgb(190, 0, 0);
else
rowheadbackcolorold = Color::FromArgb(255, 255, 255);
if (CurrColOld == CurrColNew)
dataGridView1->Columns[CurrColOld]->HeaderCell->Style->BackColor = rowheadbackcolornew;
else
dataGridView1->Columns[CurrColOld]->HeaderCell->Style->BackColor = rowheadbackcolorold;
CurrRowOld = CurrRowNew;
CurrColOld = CurrColNew;
}
自定义函数代码:
Color MyForm::getColorCell(int i, int j)
{
return dataGridView1[i, j]->Style->BackColor;
}
void MyForm::SetColor(int i, int j, const Color& newColor)
{
dataGridView1[i, j]->Style->BackColor = newColor;
}