本帖最后由 pession 于 2013-06-20 08:05:35 编辑

解决方案 »

  1.   

    这段代码应该没有问题,用个foreach循环遍历所有行就可以了
    可能是后面的代码又把颜色设置回去了
      

  2.   

    DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢?先搞懂概念吧~
      

  3.   

    注册 CellPainting事件
     void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                
            }
    在里面e.Value就是某一单元格的值  通过判断  设置颜色
      

  4.   

    你这代码是根据第四行第四列的成绩,把整个第四行颜色都修改了.
    还有我记得修改datagridview颜色方法在初始化时候调用是没用的,好像要放在form_load事件里
      

  5.   



    额,学艺不精,把行和列混淆了,贻笑大方,根据你的提示,我又写了2段代码,但是对于绑定数据库的datagridview还是无法显示出我想要的红色
    请你帮忙看看我的代码有误吗,第一段注释了,第二段没注释,还写了一个foreach的判断也没成功
           
       //private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            //{
            //    if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
            //    {
            //        object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
            //        int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
            //        if (n < 60)
            //        {
            //            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            //            cellStyle.ForeColor = Color.Red;        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
            //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
            //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //        }
            //    }
            //}
                private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
                if (intGrade < 60)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
            } //foreach (DataRow row1 in dataGridView1.Rows)
                //{
                //    DataGridViewRow gridRow = new DataGridViewRow();
                //    DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
                //    btn_cell.Value = "通过";
                //    for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
                //    {
                //        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                //        cell.Value = row1[col_index];
                //        gridRow.Cells.Add(cell);
                //        if (col_index == 3)
                //        {
                //            if (Convert.ToDecimal(row1[3]) < 60)
                //            {
                //                DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
                //                cellStyle.ForeColor = Color.Red;
                //                cell.Style = cellStyle;
                //                btn_cell.Value = "补考";
                //            }
                //        }
                //    }
                //    gridRow.Cells.Add(btn_cell);
                //    dataGridView1.Rows.Add(gridRow);
                //}
      

  6.   



    额,学艺不精,把行和列混淆了,贻笑大方,根据你的提示,我又写了2段代码,但是对于绑定数据库的datagridview还是无法显示出我想要的红色
    请你帮忙看看我的代码有误吗,第一段注释了,第二段没注释,还写了一个foreach的判断也没成功
           
       //private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            //{
            //    if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
            //    {
            //        object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
            //        int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
            //        if (n < 60)
            //        {
            //            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            //            cellStyle.ForeColor = Color.Red;        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
            //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
            //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //        }
            //    }
            //}
                private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
                if (intGrade < 60)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
            } //foreach (DataRow row1 in dataGridView1.Rows)
                //{
                //    DataGridViewRow gridRow = new DataGridViewRow();
                //    DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
                //    btn_cell.Value = "通过";
                //    for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
                //    {
                //        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                //        cell.Value = row1[col_index];
                //        gridRow.Cells.Add(cell);
                //        if (col_index == 3)
                //        {
                //            if (Convert.ToDecimal(row1[3]) < 60)
                //            {
                //                DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
                //                cellStyle.ForeColor = Color.Red;
                //                cell.Style = cellStyle;
                //                btn_cell.Value = "补考";
                //            }
                //        }
                //    }
                //    gridRow.Cells.Add(btn_cell);
                //    dataGridView1.Rows.Add(gridRow);
                //}
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.Value != null)
                {
                    string s = e.Value.ToString();
                    if (s == "111")
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                    }
                }
            }
    这个没问题  你再自己检查检查
      

  7.   

    感谢你,这个方法是对的,只是没有在设计页面datagridview事件中绑定,我更改了代码显示成功了
     private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
               // int eindex = e.RowIndex + 1;
                if (e.RowIndex > -1)
                {
                    int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
                   // MessageBox.Show(intGrade.ToString())           
                    if (intGrade < 60)
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                        this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
                    }
                }
                
            }
      

  8.   

    感谢你,这个方法是对的,只是没有在设计页面datagridview事件中绑定,我更改了代码显示成功了
     private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
               // int eindex = e.RowIndex + 1;
                if (e.RowIndex > -1)
                {
                    int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
                   // MessageBox.Show(intGrade.ToString())           
                    if (intGrade < 60)
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                        this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
                    }
                }
                
            }依此类托   一般如果想要修改控件默认显示方式(颜色、字体、样式等等)  你可以注册一些类似CellPainting的事件  在事件处理程序中  根据条件(一般e参数可以获取想要的东西)  来自定义控件的显示方式控件内部一般这样实现的:
    // ...
    DataGridViewCellPaintingEventArgs e = new DataGridViewCellPaintingEventArgs(...);
    if(CellPainting != null)
    {
        CellPainting(this,e);  //
    }
    //接着绘制控件
    //...
    你要做的:
    1.注册类似CellPaiting事件
    2.通过e参数判断是否满足条件
    3.修改显示方式
      

  9.   

    此贴终结,问题已经解决了。这里非常感谢xiaozhi_5638的回复解答,他很耐心提供了解决的思路,对待新人友好。再次感谢