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;
        }
    }
}
原文地址:https://www.cnblogs.com/zhangpengshou/p/1783213.html