Gary
Gary

Reputation: 33

Confusion regarding DataGridView CellValidation

I'm not sure if I'm misunderstanding how cell validation works, but I'm not getting the intended effect with my code. The cell fails to validate if the input isn't a valid Double, but the error text doesn't come up unless I hit escape, which returns the cell to the previous value. I want the error text to show up when the invalid input is entered, otherwise it's confusing...

EDIT: I want the error text to show up while the cell is being edited, is that possible?

private void coordDGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            double result;
            if (e.ColumnIndex >= 3)
            {
                if (!Double.TryParse(coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString(), out result))
                {
                    e.Cancel = true;
                    coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Needs to be positive or negative decimal";
                }
                else
                {
                    coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "";
                }
            }

        }    

Upvotes: 1

Views: 42

Answers (1)

Gary
Gary

Reputation: 33

I found this post which basically covered my question.

I was wondering why

this.datagridviewX.Rows[e.RowIndex].ErrorText = "Errortext";

wasn't showing up, but I realized it was because the error icon shows in the row header and I had row headers turned off! Unfortunately error icons can't be in column headers, so I reluctantly turned the row header on and it solved everything nicely!

Upvotes: 2

Related Questions