简体   繁体   中英

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.

So when my method Main() exits, here are some key values:

  • dgv.Rows[0].Cells[3].ErrorText contains "Only alpha-numeric characters allowed"
  • dgv.Visible is False
  • dgv.Rows[0].Cells[3].IsInEditMode is False

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. If I click in and then out of that cell (namely [0][3]) - the glyph appears.

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.

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. 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.

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!";
        }
    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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