winform datagridview_cellclick事件
单击某单元格,该单元格变成红色,再次单击它又回复为系统默认。我的代码逻辑有问题,该怎么改?boolflag=false;privatevoiddataGridView1_...
单击某单元格,该单元格变成红色,再次单击它又回复为系统默认。
我的代码逻辑有问题,该怎么改?
bool flag = false;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
for (int j = 4; j < this.dataGridView1.ColumnCount; j++)
{
if (e.ColumnIndex == this.dataGridView1.Columns[j].Index)
{
this.navigatorEx1.Focus();
if (flag)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (e.RowIndex == this.dataGridView1.Rows[i].Index)
{
this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
else
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (e.RowIndex == this.dataGridView1.Rows[i].Index)
{
this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Empty;
}
}
}
}
flag = !flag;
}
} 展开
我的代码逻辑有问题,该怎么改?
bool flag = false;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
for (int j = 4; j < this.dataGridView1.ColumnCount; j++)
{
if (e.ColumnIndex == this.dataGridView1.Columns[j].Index)
{
this.navigatorEx1.Focus();
if (flag)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (e.RowIndex == this.dataGridView1.Rows[i].Index)
{
this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
else
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (e.RowIndex == this.dataGridView1.Rows[i].Index)
{
this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Empty;
}
}
}
}
flag = !flag;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询