从数据库中取得数据, 然后datagridview1.datasource = table
在datagridview中进行显示
有一列是称为“比例”,现在想如果这个比例超过50%,就用红色高亮如何实现?我在网上有找到说用cell_formatting事件,但是这个怎么形成循环呢,我调试程序的时候发现这个函数只执行了一次,如何让它每行都执行一次呢?或者有什么其他更好的方法?多谢!

解决方案 »

  1.   


        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {            if (this.dataGridView1.Columns[3].HeaderText.ToString().Trim() == "比例")
                {
                    int intScale =int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                    if (this.dataGridView1.Rows[e.RowIndex].Cells[3].Value != DBNull.Value
                        && intScale>50)
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.SeaShell;
                    }
                }        }
      

  2.   


    我在程序里面加了类似你这样的函数,但是我搞不懂的是这个函数什么时候执行呢,我调试的时候发现只执行了一次,而且这一次的e.RowIndex 和e.columnIndex都为0,最后运行结果也是没有变色是不是赋数据源给datagridview的时候不能简单的用datagridview1.datasource = table ?
      

  3.   

    用DataSourceChanged事件或DataBindingComplete事件,在數據更新之後,重新改變cell的背景!