1

我想通过该代码只为空单元格着色。但我让所有的细胞都上色了。谁能告诉我我的错在哪里。。

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;
            }
        }
}
4

2 回答 2

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);
于 2013-02-05T07:38:18.830 回答
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;
    }
}
于 2013-02-05T07:39:46.210 回答