enter code here
我在网格中有两个单元格,当我输入其他更大的单元格,然后零值
时,我想将一个单元格的颜色和值搅拌为零。 private void grdDetail_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (this.IsLoadComplete)
{
UpdateValueofQtyCell(e.ColumnIndex);
}
}
private void UpdateValueofQtyCell(int index)
{
int cur_row = grdDetail.CurrentRow.Index;
if (index == 1)
{
grdDetail[1, cur_row].Style.BackColor = Color.White;
grdDetail[2, cur_row].Style.BackColor = Color.FromArgb(224, 224, 224);
grdDetail[2, cur_row].Value = 0;
}
else if (index == 1)
{
grdDetail[2, cur_row].Style.BackColor = Color.White;
grdDetail[1 cur_row].Style.BackColor = Color.FromArgb(224, 224, 224);
grdDetail[1, cur_row].Value = 0;
}
}
您可以使用此代码:
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "0")
{
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Yellow;
}
}
在类级别创建以下变量。
bool IsAnyGreaterThanZero = false;
然后首先调用下面的函数,该功能告诉任何单元格值是否大于零。
private void CheckForGreaterThanZero()
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if ((int)dataGridView1.Rows[i].Cells[0].Value > 0)
{
IsAnyGreaterThanZero = true;
}
}
}
然后调用下面的函数以更改那些具有等于零的单元格的颜色。
private void ChangeOtherCellColor()
{
if(IsAnyGreaterThanZero)
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if ((int)intdataGridView1.Rows[i].Cells[0].Value == 0)
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[0].Style = CellStyle;
}
}
}
}