foreach (DataGridViewRow dgr in dgvBuySell.Rows)
                {
                    if (dgr.Cells["BuySellStates"].Value.ToString() == "Buy") //买
                    {
                        dgr.Cells["BuySellBtnBuySell"].Value = "买入";
                        dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Red; //行变红色
                    }
                    if (dgr.Cells["BuySellStates"].Value.ToString() == "Sell") //卖
                    {
                        dgr.Cells["BuySellBtnBuySell"].Value = "卖出";
                        dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Blue; //行变蓝色
                    }
                }
我这样循环datagridview  然后判断现在有个这样的问题,如果我在datagridview的属性里设置AllowUserToAddRows = False 上面的代码就失效了,如果为True才有用,请问为什么会这样?如果要设置False如何才能让上面的代码判断有效?

解决方案 »

  1.   


    DataGridView.AllowUserToAddRows 属性 
    获取或设置一个值,该值指示是否向用户显示添加行的选项。 
      

  2.   

    关注
    不过,添加行 一般不用DataGridView.AllowUserToAddRows 属性 而是通过代码来实现
      

  3.   

    因为你设置用户不允许修改添加行 猜想就重新绘制datagridview 所以设置颜色没有起效果
      

  4.   

    你得加一个if判断单元格是否为空,AllowUserToAddRows=true后产生的那一行的值为null:foreach (DataGridViewRow dgr in dgvBuySell.Rows)
                    {
    if(dgr.Cells["BuySellStates"].Value!=null){
                        if (dgr.Cells["BuySellStates"].Value.ToString() == "Buy") //买
                        {
                            dgr.Cells["BuySellBtnBuySell"].Value = "买入";
                            dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Red; //行变红色
                        }
                        if (dgr.Cells["BuySellStates"].Value.ToString() == "Sell") //卖
                        {
                            dgr.Cells["BuySellBtnBuySell"].Value = "卖出";
                            dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Blue; //行变蓝色
                        }
    }
                    }
      

  5.   

    你是在那个事件里面写的代码?dataGridView_CellPainting?
      

  6.   

    加了 if(dgr.Cells["BuySellStates"].Value!=null){ } 也不行啊,还是无效
      

  7.   

    你这个是写在什么事件里了啊?像这种需求一般写在CellFormating里比较合适
      

  8.   

    我没有放在CellFormating事件里,那样控件会狂闪,好奇怪!
      

  9.   

    你上面列出的代码,是放在什么事件里呢?若放在CellFormating事件里,可这样写,不需循环: private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    if (e.Value != null)
                    {
                        if (e.Value.ToString() == "XB")
                        {
                            e.CellStyle.BackColor = Color.Red;
                        }
                        else
                        {
                            e.CellStyle.BackColor = Color.Blue;
                        }
                    }
                }            
            }
      

  10.   

    理想的是放在CellFormatting事件里,但代码要改,不能采用循环全部,因为CellFormatting事件是每一行触发.
    想一下每CellFormatting事件就要去遍列所有行,整个显示下来就要重复N多次,当然会狂闪了!按下面代码的思路去调试一下吧,祝你成功!
    private void dgvBuySell_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dgvBuySell[e.RowIndex]["BuySellStates"].ToString()== "Buy")
                {
                     dgvBuySell[e.RowIndex]["BuySellBtnBuySell"].Value = "买入";
                    e.CellStyle.BackColor = System.Drawing.Color.Red; //当前行变红色       
                }
              if (dgvBuySell[e.RowIndex]["BuySellStates"].ToString()=="Sell")
                {
                      dgvBuySell[e.RowIndex]["BuySellBtnBuySell"].Value = "卖出";
                    e.CellStyle.BackColor = System.Drawing.Color.Blue; //当前行行变蓝色
                  }
          
     }
      

  11.   

    dgvBuySell[e.RowIndex]["BuySellStates"]这样写不对哦
      

  12.   

    以上更正为:
    dgvBuySell["BuySellStates",e.RowIndex].Value
      

  13.   

     DataView 取值 dv_tmp[RecordIndex]["BuySellStates"].ToString()
     DataGridView 取值 dgvBuySell["BuySellStates",RowIndex].Value