3

GridView从 excel 导入我需要在每个空单元格附近显示一条消息,以向用户提供有关它应该写什么的信息..

void simpleButton1_Click(object sender, System.EventArgs e)
{
    string[] msg = new string[60];
    string[] error = new string[400];
    for (int i = 0; i < gridView3.RowCount ; i++)
    {
        System.Data.DataRow Rows = gridView3.GetDataRow(i);
        string cellvalue = Rows[0].ToString();
        if (cellvalue == "")
        {
            msg[0] = "Missing 'First Name'";
            error[i] = msg[0] + " - ";  
        }   
        cellvalue = Rows[1].ToString();
        if (cellvalue == "")
        {
            msg[1] = "Missing 'Last Name'";
            error[i] += msg[1] + " - ";
        }
        //...
    }
}

如何将变量放入msg[]带有小图像或"!"图形的特定单元格,或者我可以为单元格着色

4

4 回答 4

2

更改单元格的颜色

Rows[1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
于 2013-01-30T12:02:07.570 回答
2

您可以使用条件格式功能为 XtraGrid 单元格着色:

gridControl1.DataSource = new List<Person> { 
    new Person(){ Name = "John", Age = 25 },
    new Person(){ Name = "Mary", Age = 17 },
    new Person(){ Age = 17  },
    new Person(){ Name = "Ann" },
    new Person(){ Name = "Pit", Age = 5 },
};
StyleFormatCondition nameCondition = new StyleFormatCondition();
nameCondition.Column = gridView1.Columns["Name"];
nameCondition.Condition = FormatConditionEnum.Expression;
nameCondition.Expression = "IsNullOrEmpty([Name])";
nameCondition.Appearance.BackColor = Color.Red;
nameCondition.Appearance.Options.UseBackColor = true;

StyleFormatCondition ageCondition = new StyleFormatCondition();
ageCondition.Column = gridView1.Columns["Age"];
ageCondition.Condition = FormatConditionEnum.Expression;
ageCondition.Expression = "[Age]<10";
ageCondition.Appearance.BackColor = Color.Maroon;
ageCondition.Appearance.Options.UseBackColor = true;

gridView1.FormatConditions.AddRange(new StyleFormatCondition[] { 
    nameCondition, ageCondition
});

结果:
XtraGrid 条件格式

相关链接:
自定义单个行和单元格的外观
样式格式条件
自定义绘画(示例)

于 2013-01-30T12:57:45.983 回答
1

参考:如何:为数据单元格提供自定义显示文本

通过 ColumnView.CustomColumnDisplayText事件为数据单元格提供自定义显示文本。有关自定义绘图和单元格样式的更多信息,请参阅自定义绘画样本自定义单个行和单元格的外观 文档部分。

检查示例空字符串是否显示在“折扣”列的单元格中,如果它们包含零值。

using DevExpress.XtraGrid.Views.Base;

private void gridView1_CustomColumnDisplayText(object sender, 
CustomColumnDisplayTextEventArgs e) {
   if(e.Column.FieldName == "Discount")
      if(Convert.ToDecimal(e.Value) == 0) e.DisplayText = "";
}

如果要同时显示Image and text两者,则需要处理 GridView 的GridView.CustomDrawCell事件,这里是根据其他列值(年龄列)更改名称列颜色的代码片段

private void gridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
    {
        if (e.Column == colName)
        {
            var age = Convert.ToInt32(gridView.GetRowCellValue(e.RowHandle, colAge));
            if (age < 18)
                e.Appearance.BackColor = Color.FromArgb(0xFE, 0xDF, 0x98);
            else
                e.Appearance.BackColor = Color.FromArgb(0xD2, 0xFD, 0x91);
        }
    }
于 2013-01-30T13:10:06.693 回答
1

也许您可以使用 aToolTip来显示您的警报:

toolTip1.ToolTipIcon = ToolTipIcon.Warning;
toolTip1.ToolTipTitle = "Warning!";
toolTip1.Show("Missing 'First Name'", x, y);

您只需要根据DataGridView.

示例 1 示例 2

ToolTipSystem.Windows.Forms命名空间中。

于 2013-01-30T12:12:11.840 回答