【问题标题】:Show message in empty cells in GridView在 GridView 的空单元格中显示消息
【发布时间】:2013-01-14 05:35:02
【问题描述】:

我正在从 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[] 放入带有小图像或 "!" 图形的特定单元格,或者我可以为单元格着色

【问题讨论】:

  • 也许你可以使用ToolTip
  • ASPxGridView?超网格? DX网格?您是否尝试在Devex Support 中发布此问题?
  • 不,我没有……我使用的是 XtraGrid……不是 ASP.net
  • System.Data.DataRow 已经包含一个属性,该属性指示通常为空白的行 (System.Data.DataRow.RowError) 存在错误。如果您想指出该行存在错误,可以将其更改为错误文本。当此属性不为空时,该行旁边应出现一个感叹号。祝你有美好的一天:)

标签: c# winforms devexpress xtragrid


【解决方案1】:

改变单元格的颜色

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

【讨论】:

  • 我正在寻找能够达到我的观点的任何解决方案。我需要能够让用户知道需要修改哪个单元格并显示有关此空单元格的消息@ Arun CB,你写的是对的。谢谢你,但我仍然需要这个信息
【解决方案2】:

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

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

您只需要根据DataGridView的行列大小来猜测单元格的位置即可。

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

【讨论】:

  • ToolTipSystem.Windows.Forms 命名空间中。
  • 我认为这是个坏主意。你知道我有 50 列我不知道单元格的位置。但我非常感谢你的帮助
  • 您可以根据从DataGridView 获得的值计算单元格的位置。你可以创建一个函数来告诉你位置。
【解决方案3】:

您可以使用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
});

结果:

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

【讨论】:

  • 我现在要试试这个方法,如果一切顺利,请告诉你。谢谢
  • +1 用于单元格格式,@Nej 上面的代码用于空单元格的背景颜色更改。
【解决方案4】:

参考: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 事件,这里有一段代码可以根据其他列值(年龄列)更改名称列的颜色)

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

【讨论】:

  • 为什么我的事件中没有 CustomColumnDisplayText ?你有什么主意吗 ?提前谢谢..
  • View有这个事件,不是GridControl
  • 使用gridView1.CustomColumnDisplayText += event handler
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多