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.
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;
}
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With