C# 在GridView的空单元格中显示消息

C# 在GridView的空单元格中显示消息,c#,winforms,devexpress,xtragrid,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++) {

我正在从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] + " - ";
        }
        //...
    }
}
void simpleButton1\u单击(对象发送方,System.EventArgs e)
{
字符串[]消息=新字符串[60];
字符串[]错误=新字符串[400];
对于(int i=0;i

如何将变量
msg[]
放入带有一点图像或
“!”
图形的特定单元格,或者我可以给单元格上色来更改单元格的颜色

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

也许您可以使用
工具提示来显示您的警报:

toolTip1.ToolTipIcon = ToolTipIcon.Warning;
toolTip1.ToolTipTitle = "Warning!";
toolTip1.Show("Missing 'First Name'", x, y);
您只需要根据
DataGridView
的行和列的大小猜测单元格的位置


工具提示
位于
System.Windows.Forms
命名空间中。

您可以使用以下功能为XtraGrid单元格着色:

gridControl1.DataSource=新列表{
新人(){Name=“John”,年龄=25},
新人(){Name=“Mary”,年龄=17},
新人(){Age=17},
新人(){Name=“Ann”},
新人(){Name=“Pit”,年龄=5},
};
StyleFormatCondition nameCondition=新建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=新建StyleFormatCondition();
ageCondition.Column=gridView1.Columns[“Age”];
ageCondition.Condition=FormatConditionEnum.Expression;
ageCondition.Expression=“[Age]参考:

通过为数据单元格提供自定义显示文本的步骤 事件。以获取更多信息 关于自定义绘图和单元格样式, 文件科

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

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 = "";
}
如果您想同时显示
图像和文本
,则需要处理GridView的事件,下面是一段代码,它根据另一列valoe(年龄列)更改名称列的颜色

private void gridView\u CustomDrawCell(对象发送方,RowCellCustomDrawEventArgs e)
{
if(e.Column==colName)
{
var age=Convert.ToInt32(gridView.GetRowCellValue(e.RowHandle,colAge));
if(年龄<18岁)
e、 外观.BackColor=Color.FromArgb(0xFE,0xDF,0x98);
其他的
e、 外观.BackColor=Color.FromArgb(0xD2,0xFD,0x91);
}
}

也许你可以使用
工具提示
ASPxGridView?XtraGrid?DXGrid?你试过在中发布这个问题吗?没有。我用的是XtraGrid..不是ASP.net
System.Data.DataRow
已经包含一个属性,表明行有错误(
System.Data.DataRow.rower
)通常为空。如果您想指出该行有错误,可以将其更改为错误文本。当此属性不为空时,该行旁边应显示感叹号。祝您愉快:)我正在搜索任何解决方案以达到我的目的..我需要能够让用户知道哪个单元格是空的ed需要修改并显示有关此空单元格的消息@Arun C.B您写的是对的谢谢但我仍然需要消息
工具提示
位于
System.Windows.Forms
命名空间中。我认为这是个坏主意。您知道我有50列,我无法知道单元格的位置。但是我非常感谢您的帮助您可以根据从
DataGridView
中获得的值计算单元格的位置。您可以创建一个函数来告诉您位置。为什么我的事件中没有CustomColumnDisplayText?您知道吗?提前谢谢..View拥有此事件,而不是GridControluse
gridView1.CustomColumnDisplayText+=事件处理程序
。我现在将尝试此方法,并让您知道是否一切正常..感谢+1的单元格格式设置,@nej上述代码用于空单元格的背景颜色更改。
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 = "";
}
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);
        }
    }