无法获得仅有色的空单元格

时间:2013-02-05 07:06:32

标签: c# devexpress

我希望通过该代码仅为空单元格着色。 但我让所有的细胞都变色了。 谁能告诉我哪里是我的错......

private void gridView3_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
        GridView View = sender as GridView;
        if(e.Column.FieldName == "First Name") 
        {
            string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["First Name"]);
            if(category != "") 
            {   
                e.CellStyle.BackColor = Color.DeepSkyBlue;
            }
        }
}

2 个答案:

答案 0 :(得分:3)

一方面,情况有误:

if (category != "")

您正在进行过滤,以便非空的单元格变为彩色。所以先用String.IsNullOrEmpty(category)替换它。

接下来,因为您使用的是DevExpress控件,所以您可以使用内置的条件格式:http://documentation.devexpress.com/#WindowsForms/CustomDocument759

这样的事情可能是:

StyleFormatCondition condition1 = new DevExpress.XtraGrid.StyleFormatCondition();
condition1.Appearance.BackColor = Color.DeepSkyBlue;
condition1.Appearance.Options.UseBackColor = true;
condition1.Condition = FormatConditionEnum.Expression;
condition1.Expression = "[First Name] == ''";
gridView1.FormatConditions.Add(condition1);

答案 1 :(得分:2)

您的代码正在为非空单元格着色(请检查您的条件if(category != "")

以下适用于我:

private void gridView3_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
    GridView View = sender as GridView;
    if (e.Column.FieldName == "First Name" &&
        string.IsNullOrEmpty(View.GetRowCellDisplayText(e.RowHandle, e.Column)))
    {
        e.Appearance.BackColor = Color.DeepSkyBlue;
    }
}