在GridView中以空单元格显示消息

时间:2013-01-30 11:55:45

标签: c# winforms devexpress xtragrid

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

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 个答案:

答案 0 :(得分:2)

更改单元格的颜色

Rows[1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

答案 1 :(得分:2)

您可以使用Conditional Formatting功能

为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 Conditional Formatting

相关链接:
Customizing Appearances of Individual Rows and Cells
Style Format Conditions
Custom Painting (Samples)

答案 2 :(得分:1)

也许您可以使用ToolTip来显示提醒:

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

您只需根据DataGridView的行和列的大小猜测单元格的位置。

Example1 Example2

ToolTip位于System.Windows.Forms名称空间中。

答案 3 :(得分:1)

参考:How to: Provide Custom Display Text for Data Cells

  

通过提供数据单元格的自定义显示文本   ColumnView.CustomColumnDisplayText事件。了解更多信息   关于自定义绘制和单元格样式,请Custom Painting SamplesCustomizing Appearances of Individual Rows and Cells   文档部分。

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

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事件,这里有一段代码,可以根据其他列更改Name列的颜色valoe(年龄栏)

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