简体   繁体   English

强制 ErrorText 显示在 DataGridView 中

[英]Forcing ErrorText to show in a DataGridView

I've Googled and Googled for this already...我已经用谷歌和谷歌搜索过这个......

When my application starts, it loads a config file and displays its contents in a DataGridView - including errors found in the config file.当我的应用程序启动时,它会加载一个配置文件并在 DataGridView 中显示其内容 - 包括在配置文件中发现的错误。

So when my method Main() exits, here are some key values:所以当我的方法 Main() 退出时,这里有一些关键值:

  • dgv.Rows[0].Cells[3].ErrorText contains "Only alpha-numeric characters allowed" dgv.Rows[0].Cells[3].ErrorText包含“只允许使用字母数字字符”
  • dgv.Visible is False dgv.Visible是假的
  • dgv.Rows[0].Cells[3].IsInEditMode is False dgv.Rows[0].Cells[3].IsInEditMode为假

Here's the pertinent code:这是相关的代码:

    public Main()
    {
        InitializeComponent();
        dgvStationConfiguration.DataSource = FileReaderWriter.GetStationsFromConfigFile();
        StationConfigurationValidator.ValidateAllCellsAndSetAllErrorMessages(dgvStationConfiguration);
    }

and

    public static bool ValidateAllCellsAndSetAllErrorMessages(DataGridView dgv)
    {
        bool areAllCellsValid = true;

        foreach (DataGridViewRow row in dgv.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                bool isCellValid = ValidateCellAndSetErrorMessage(cell); // validate all cells in order to set their error text/glyphs; this flag is just to be able to return a summary

                if (isCellValid == false)
                {
                    areAllCellsValid = false;
                }
            }
        }

        return areAllCellsValid;
    }

and

    public static bool ValidateCellAndSetErrorMessage(DataGridViewCell cell)
    {
        string columnName = cell.OwningColumn.Name;
        string cellValue = cell.EditedFormattedValue.ToString();

        cell.ErrorText = StationConfigurationValidator.GetCellErrorMessage(columnName, cellValue);
        return cell.ErrorText == string.Empty;
    }

When the method completes and the user is shown the DataGridView, no red error glyphs are visible.当方法完成并向用户显示 DataGridView 时,看不到红色错误字形。 If I click in and then out of that cell (namely [0][3]) - the glyph appears.如果我点击进入然后退出该单元格(即 [0][3]) - 字形出现。

I get the impression that the main problem is that when the ErrorText is set (in method Main) the the DataGridView is still not visible.我的印象是,主要问题是当设置 ErrorText 时(在方法 Main 中),DataGridView 仍然不可见。

I'm getting so desperate that I'm thinking of this incredible hack: have a timer go off in 10ms (to allow method Main to exit) to set the ErrorText - then disable (unhook) the timer.我变得如此绝望,以至于我想到了这个令人难以置信的黑客:让计时器在 10 毫秒内关闭(以允许 Main 方法退出)以设置 ErrorText - 然后禁用(取消挂接)计时器。 That's such a hack I can't stand it... Just illustrating my desperation... :-(这是一个我无法忍受的黑客......只是说明我的绝望...... :-(

So... What do I need to do to make that glyph show???所以......我需要做什么才能让那个字形显示???

Place your datagrid validation code in the Load event rather than in your constructor and the glyphs will show up right away with no need for handling the VisibleChanged event.将数据网格验证代码放在Load事件中,而不是在构造函数中,字形将立即显示,无需处理VisibleChanged事件。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<KeyValuePair<int, string>> list1 = new List<KeyValuePair<int, string>>();
        list1.Add(new KeyValuePair<int, string>(1, "string 1"));
        list1.Add(new KeyValuePair<int, string>(2, "string 2"));
        list1.Add(new KeyValuePair<int, string>(3, "string 3 is too long."));
        list1.Add(new KeyValuePair<int, string>(4, "string 4"));
        list1.Add(new KeyValuePair<int, string>(5, "string 5"));

        dataGridView1.DataSource = list1;
        DgvValidator();
    }

    private void DgvValidator()
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (((string)row.Cells[1].Value).Length > 10)
                row.Cells[1].ErrorText = "ERROR!";
        }
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM