user2025696
user2025696

Reputation: 159

While using the DataError event how does one get e.Context to be parsed?

I currently have a datagridview that a user uses to enter data. In order to catch bad formatting i searched and read that the event DataError was the way to go. The few samples i do find all seem to think that the e.Context has a single DataGridViewDataErrorContext like so.

if (e.Context == DataGridViewDataErrorContexts.Commit)    
{    
    MessageBox.Show("Commit error");    
}

But when i try to implement the same thing on a test grid i get the following for my e.Context instead. It has a list like so: Parsing | Commit | CurrentCellChange

Screenshot of e.Context

Notice that the value for e.Context is a list of values and not a single one like the sample code online show it to be. Am i doing something incorrectly or missing a step? How does one have that seperated?

I'm using VS 2010 Prof.

Thanks in advance!

Upvotes: 0

Views: 1567

Answers (2)

FOR
FOR

Reputation: 4368

As of .NET Framework 4, I think the preferred way is to use the Enum.HasFlag method. This, of course, should be used with Enum types that are marked with the [Flags] attribute (DataGridViewDataErrorContexts is one of them).

I just recently wrote this little helper method, which you may find helpful:

private string ReadableDataGridViewContext(DataGridViewDataErrorContexts context)
{
    var translations = new Dictionary<DataGridViewDataErrorContexts, string> {
        { DataGridViewDataErrorContexts.ClipboardContent, "Copying Data to the Clipboard" },
        { DataGridViewDataErrorContexts.Commit, "Committing Data" },
        { DataGridViewDataErrorContexts.CurrentCellChange, "Moving Focus to a different Cell, due to error in the Cell being left" },
        { DataGridViewDataErrorContexts.Display, "Displaying Data in a Cell" },
        { DataGridViewDataErrorContexts.Formatting, "Formatting Data" },
        { DataGridViewDataErrorContexts.InitialValueRestoration, "Restoring Cell Data" },
        { DataGridViewDataErrorContexts.LeaveControl, "Leaving the Grid" },
        { DataGridViewDataErrorContexts.Parsing, "Parsing Data" },
        { DataGridViewDataErrorContexts.PreferredSize, "Calculating the preferred size for a Cell" },
        { DataGridViewDataErrorContexts.RowDeletion, "Deleting a Row" },
        { DataGridViewDataErrorContexts.Scroll, "Scrolling over the Grid" }
    };

    var list = (from contextFlag in translations.Keys
                    where context.HasFlag(contextFlag)
                    select translations[contextFlag]).ToList();
    return String.Join(",", list);
}

Upvotes: 0

user2025696
user2025696

Reputation: 159

Not sure it was the correct path to take but I ended up modifying it slightly to get it to work.

    //if (e.Context == DataGridViewDataErrorContexts.Commit)
    if (e.Context.ToString().Contains(DataGridViewDataErrorContexts.Commit.ToString()))
    {

              MessageBox.Show("Commit error");
    }

    //if (e.Context == DataGridViewDataErrorContexts.CurrentCellChange)
    if (e.Context.ToString().Contains(DataGridViewDataErrorContexts.CurrentCellChange.ToString()))
    {

              MessageBox.Show("Cell change");
    }

    //if (e.Context == DataGridViewDataErrorContexts.Parsing)
    if (e.Context.ToString().Contains(DataGridViewDataErrorContexts.Parsing.ToString()))
    {

              MessageBox.Show("Parsing error");
    }

    //if (e.Context == DataGridViewDataErrorContexts.LeaveControl)
    if (e.Context.ToString().Contains(DataGridViewDataErrorContexts.LeaveControl.ToString()))
    {

              MessageBox.Show("Leave control error");
    }

This allows me to check what is in e.Context

Upvotes: 1

Related Questions