Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Get the cell text value if DataError is triggered

I have a DataGridView and I populate it dynamically from my database via the following code

DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.HeaderText = "id";
colID.DataPropertyName = "id";
colID.ReadOnly = true;
colID.Visible = false;
dtgvLoadEx.Columns.Add(colID);

DataGridViewTextBoxColumn colLoadExpiryDate = new DataGridViewTextBoxColumn();
//CalendarColumn colLoadExpiryDate = new CalendarColumn();
colLoadExpiryDate.HeaderText = "LoadExpiryDate(mm/dd/yy)";
colLoadExpiryDate.Width = 158;
colLoadExpiryDate.DataPropertyName = "LoadExpiryDate";
colLoadExpiryDate.ReadOnly = false;
colLoadExpiryDate.MaxInputLength = 10;
dtgvLoadEx.Columns.Add(colLoadExpiryDate);

dtgvLoadEx.DataSource = data(); //Return data table from my Database

As you can see I have a Date column. When I attempt to edit a cell of that column and type an invalid format, the DataError event will triggered.

Now I just want to get the error text from

private void dtgvLoadEx_DataError(object sender, DataGridViewDataErrorEventArgs e) {

}

or any other process in order to get the error text.

like image 997
user2530833 Avatar asked Jul 17 '13 10:07

user2530833


3 Answers

Ok guys I Already Solved the Problem. Here I'm gonna share it

 private void dtgvLoadEx_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            string s = dtgvLoadEx.EditingControl.Text;
        }
like image 90
user2530748 Avatar answered Nov 06 '22 22:11

user2530748


You want to use the event argument as follows

string errorText;
private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    // This will return the last text entry that was not erroneous.
    string cellValue = dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();

    // This will get the text indicating the error condition.
    errorText = dataGridView[e.ColumnIndex, e.RowIndex].ErrorText;
}

Edit. Having read your comment below, if the first of the above is not returning the erroneous striing value, then it might not be possible. Try using ErrorText instead.

I hope this helps.

like image 43
MoonKnight Avatar answered Nov 06 '22 21:11

MoonKnight


You could do a custom validation using CellValidating event:

private void dataGridView_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
    if (!DateTime.TryParse(e.FormattedValue))
    {
         string s = e.FormattedValue;
         e.Cancel = true;
    }
}
like image 41
Giannis Paraskevopoulos Avatar answered Nov 06 '22 21:11

Giannis Paraskevopoulos