用C# 怎样使GridView中的某行背景色变红色或字体变为蓝色?而其它行保存默认色?

解决方案 »

  1.   

       DataGridViewCellStyle style;
    style = new DataGridViewCellStyle();
                style.BackColor = Color.Red;
                for (int j = 0; j < DGVAnalyse.Rows.Count; j++)
                {
                    if (DGVAnalyse.Rows[j].Cells[4].Value.toints() != DGVAnalyse.Rows[j].Cells[5].Value.toints())
                    {
                        DGVAnalyse.Rows[j].DefaultCellStyle = style;
    }
    }
    是winform的datagridview
      

  2.   

    private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 

    if (e.RowIndex >= dataGridView1.Rows.Count - 1) 
    return; 
    DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex]; 
    try 

    //dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值 
    if (dgr.Cells[0].Value.ToString() == "男") 

    //定义画笔,使用颜色是深灰。 
    using (SolidBrush brush = new SolidBrush(Color.DarkGray)) 

    //利用画笔填充当前行 
    e.Graphics.FillRectangle(brush, e.RowBounds); 
    //将值重新写回当前行。 
    e.PaintCellsContent(e.ClipBounds); 
    e.Handled = true; 



    catch (Exception ex) 

    MessageBox.Show(ex.Message); 
    } }
      

  3.   

            private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
                if (e.RowIndex >= dataGridView1.Rows.Count - 1)
                return;
                DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];
                try
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        //dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值 
                        if (dgr.Cells[1].Value.ToString() == "使用中")
                        {
                            dgr.DefaultCellStyle.BackColor = Color.Red;
                        }
                        else
                        {
                            dgr.DefaultCellStyle.BackColor = Color.Green;
                        }
                    }
                }
                catch (Exception)
                {
                }
    }
      

  4.   

    private   void   dataGridView1_RowPrePaint(object   sender,   DataGridViewRowPrePaintEventArgs   e) 
                    { 
                            if   (e.RowIndex   > =   dataGridView1.Rows.Count   -   1) 
                                    return; 
                            DataGridViewRow   dgr   =   dataGridView1.Rows[e.RowIndex]; 
                            try 
                            { 
                                    if   (dgr.Cells[" "].Value.ToString()=="A ") 
                                    { 
                                            dgr.DefaultCellStyle.ForeColor   = Color.Red                                } 
                            } 
                            catch   (Exception   ex) 
                            { 
                                    MessageBox.Show(ex.Message); 
                            } 
                    } 
      

  5.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e != null && e.RowIndex > -1)
                {
                    DataGridViewRow dr = this.dataGridView1.Rows[e.RowIndex];
                    string i= dr.Cells[""].Value;
                    if (i=="1")
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Gray;
                    }               
                    if (i== "2")
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Gray;
                    }
                } 
            }
      

  6.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
             //鼠标移到GridView某一行如何改变该行的背景色   
             if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
           {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ccccff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
           }
        }