0

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

2 回答 2

1

您可以使用以下代码:

    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;
        }
    }
于 2012-11-07T06:34:02.867 回答
0

在类级别创建以下变量。

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


  }
于 2012-11-07T06:45:01.603 回答