DataGridView 设置行的颜色

1.webform
在DataGridView的RowDataBound事件裡判斷並修改:
if(e.Row.Cells[n].Text== "0 ")
{
      e.Row.Attributes.Add( "bgColor ",   "red ");
}
else   if(e.Row.Cells[n].Text> "500 ")
{
        e.Row.Attributes.Add( "bgColor ",   "green ");
}
//這裡的n是你的列qty的下標值
2.winform
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()   ==   "比较值 ")
                                {
                                        dgr.DefaultCellStyle.ForeColor   =   设置的颜色;
                                }
                        }
                        catch   (Exception   ex)
                        {
                                MessageBox.Show(ex.Message);
                        }
                }


//如果 "小明 "在第一列
DataGridViewCellStyle   style   =   new   DataGridViewCellStyle();
foreach(DataGridViewRow   aRow   in   dataGridView1.Rows)
{
if(aRow.Cells[0].Value.ToString()   ==   "小明 ")
{
style.BackColor=Color.Blue;
aRow.DefaultCellStyle=style;
}
}


或者添加CellPainting事件,比如:
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;
}
}
}

分别设置每一个列的DefaultCellStyle   属性就可以了,比如下面的代码:

DataGridViewCellStyle   dataGridViewCellStyle1   =   new   DataGridViewCellStyle();
dataGridViewCellStyle1.Alignment   =   DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor   =   Color.AliceBlue;
dataGridViewCellStyle1.ForeColor   =   Color.Red;
dataGridViewCellStyle1.SelectionBackColor   =   Color.Fuchsia;
dataGridViewCellStyle1.SelectionForeColor   =   Color.Yellow;
this.fParentDataGridViewTextBoxColumn.DefaultCellStyle   =   dataGridViewCellStyle1;

想必上面的代码你很清楚,DataGridView控件无法实现你的需求,两个解决办法:
1、自己扩展DataGridView控件,使之实现你的要求。
2、找第三方控件。
      这里我用过DevExpress的GridControl控件,功能非常强大,完全可以实现这个功能,我给出部分代码,你看看就明白了:(它可以直接设置每个单元格的外观)

using   DevExpress.XtraGrid.Views.Grid;
//   ...
private   void   gridView1_RowStyle(object   sender,   DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs   e)   {
      GridView   view   =   sender   as   GridView;
      if(e.RowHandle   >=   0)   {
            string   category   =   view.GetRowCellDisplayText(e.RowHandle,   view.Columns["Category"]);
            if(category   ==   "Beverages")   {
                  e.Appearance.BackColor   =   Color.Salmon;
                  e.Appearance.BackColor2   =   Color.SeaShell;
            }                        
      }
}

原文地址:https://www.cnblogs.com/voidxy/p/1587714.html