在gridview devexpress中更改单元格的颜色而不是行

时间:2016-07-31 08:26:52

标签: c# gridview devexpress

我有grid view devexpress c#显示大量数据,我需要根据数据值设置单元格的颜色,如下所示:

  private void gridView_RowStyle_1(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {


                // Some condition
                if (gridView.GetRowCellValue(e.RowHandle, gridView.Columns["Id"]).ToString() == "2")
                {
                    e.Appearance.BackColor = Color.Green;
                }
            }
        }

但是这个函数改变了整个行颜色而不是单元格。如何设置单元格的颜色?

1 个答案:

答案 0 :(得分:0)

使用GridView.RowCellStyle事件如下:

    void gridView1_RowCellStyle( object sender, RowCellStyleEventArgs e)
    {
       GridView currentView = sender as GridView;
       if (e.Column.FieldName == "Customer" )
       {
          bool value = Convert.ToBoolean(currentView.GetRowCellValue(e.RowHandle, "Flag_Customer" ));
           if (value)
           e.Appearance.BackColor = Color.Red;
       }
       if (e.Column.FieldName == "Vendor" )
       {
          bool value = Convert.ToBoolean(currentView.GetRowCellValue(e.RowHandle, "Flat_Vendor" ));
          if (value)
          e.Appearance.BackColor = Color.Red;
       }
    }