DataGridView改变行颜色

 例一:

private void dataGridView1_RowPrePaint(object   sender,   DataGridViewRowPrePaintEventArgs   e)
{
    
if(e.RowIndex < dataGridView1.Rows.Count -1)
    {
        DataGridViewRow dgrSingle 
= dataGridView1.Rows[e.RowIndex];
        
try
        {
            
if(dgrSingle.Cells["列名"].Value.ToString().Contains("比较值"))
            {
                dgrSingle.DefaultCellStyle.ForeColor 
= Color.Red;
            }
        }
        
catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

 例二:

 

private void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
    
if(e.RowIndex == 2 && e.ColumnIndex> =0)
    {
        
using(SolidBrush brush = new SolidBrush(Color.Red))
        {
            e.Graphics.FillRectangle(brush,e.CellBounds);
            e.Handled 
= true;
        }
    }
}

 

 

posted on 2010-07-22 17:49  一路前行  阅读(10481)  评论(0编辑  收藏  举报