Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Making DataGridView DataError set column error text
 

Making DataGridView DataError set column error text

I have a data grid view bound to a data view. I'd like the failed validation effect for the user to be the same whether it comes from the underlying data table's column changing event or the DataGridView's DataError event -- to set an error provider on the cell and let them deal with it in thier own way.

In the DataError event while I can set the ErrorText, it doesn't appear to do anything at all -- the user is still "trapped" in the cell with the error. All the examples I've been able to find display a message box. Is it possible to set the error text in the cell and advance the focus to the next logical cell?

Thanks, and if any MS folks read this the new grid is a huge improvement over the old one.

Alan

Alan Robbins  Wednesday, September 06, 2006 8:29 PM

Do you try set e.cancel=false after you set the ErrorText?

Canceling the textbox’s Validating event prevents the focus from leaving the textbox. 
This forces the user to enter good data into the textbox in order to continue.

Wang Chi  Thursday, September 07, 2006 4:52 AM

From the lack of replies apparently you can't change the way that Data Error works, which is a real shame.

<Rant>

The existing behaviour, IMHO, makes it more difficult to do a proper three tier architecture...

The way it used to work, one could have the data table / data set in the middle tier, and perform all the data validation in the data column changing & row changing events of the data table. That way, no matter how many different presentations existed, they could all share the same validation logic. Now with some of the error handling in the Data Error event,and some of the error handling in data column changing and row changing, the distinction between which tier is responsible for what blurs a little.

Trapping the user is a good way to get a three finger restart. By trapping the user I mean putting him/her in a situation where s/he must deal with an error before focus can advance to the next control. That is, however, exactly how the data error thinking goes, the system says "This column is a number, and thou shalt type in a number and that's final."

</Rant>

Oh well!

Alan Robbins  Monday, September 11, 2006 1:24 PM

Do you try set e.cancel=false after you set the ErrorText?

Canceling the textbox’s Validating event prevents the focus from leaving the textbox. 
This forces the user to enter good data into the textbox in order to continue.

Wang Chi  Thursday, September 07, 2006 4:52 AM

Wang

That works for most errors so good answer...But I think the problem occurs farther up the sink chain (or message pump queue)...

I believe the issue is that the underlying data source column is an Int32, so if the user types 1.5 in the grid cell the type converter built into the grid's formatting routines can't coerce that into an integer, and that is what is calling the DataError event, so by the time one gets to that event no cancellation is possible.

A work around would be to create an unbound string column on the fly, move the ints to the string column, and move the string into the Int column when the row change committs... but what the user's want is:

They type whatever they want into the cell.

If it's wrong they get an error provider.

If there's an error provider the new value is not committed to the data store until corrected.

Is it possible to over-ride the behaviour of the default type converter?

Alan

Alan Robbins  Thursday, September 07, 2006 12:42 PM

From the lack of replies apparently you can't change the way that Data Error works, which is a real shame.

<Rant>

The existing behaviour, IMHO, makes it more difficult to do a proper three tier architecture...

The way it used to work, one could have the data table / data set in the middle tier, and perform all the data validation in the data column changing & row changing events of the data table. That way, no matter how many different presentations existed, they could all share the same validation logic. Now with some of the error handling in the Data Error event,and some of the error handling in data column changing and row changing, the distinction between which tier is responsible for what blurs a little.

Trapping the user is a good way to get a three finger restart. By trapping the user I mean putting him/her in a situation where s/he must deal with an error before focus can advance to the next control. That is, however, exactly how the data error thinking goes, the system says "This column is a number, and thou shalt type in a number and that's final."

</Rant>

Oh well!

Alan Robbins  Monday, September 11, 2006 1:24 PM

Hi Alan.

I have a problem like yours.

I set the ErrorText but it doesn't appearand the user is still "trapped" in the cell without any message.

regards

Andrea

netit  Thursday, May 15, 2008 6:52 PM

I don't know if this will help guy's but I use this in the DataError event:

((DataGridView)sender).CancelEdit();

after the error if the user removes focus from the row the data is discarded.

ltheONEl  Monday, June 16, 2008 9:45 PM

That could be a good idea. Display an error, let the user focus another cell, and set the original value.

Code Snippet

e.ThrowException = false;

DataGridView dataGridView = ((DataGridView)sender);
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "The value entered was wrong. The original value is reset";
dataGrid.CancelEdit();

luisnike19  Friday, December 05, 2008 7:01 PM

Thanks! I tried this idea in vb.net and it works

Here is the code snipet in vb.net

e.ThrowException =

False

Dim v As DataGridView = CType(sender, DataGridView)

v.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText =

"The value entered was wrong. The original value is reset"

v.CancelEdit()

ShaluD  Tuesday, May 05, 2009 3:11 PM
Hi ,

I have an integer column in DataGridView and i dont want to CancelEdit when the user enternon numirical value, insted i need to force error icon to apear after

e.ThrowException = False

how can i do it??

My code is:



 Private Sub DataGridView1_CellValidating(ByVal sender As Object,  _ 
ByVal e As DataGridViewCellValidatingEventArgs)  _ 
Handles DataGridView1.CellValidating

        If DataGridView1.Columns(e.ColumnIndex).Name = "NumiricText1" Then
                       Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
            Dim newInteger As Integer

             If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return
            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
                OrElse newInteger < 0 Then
                e.Cancel = True
                Me.DataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"
            End If

        End If

    End Sub

Private Sub DataGridView1_DataError(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
 Handles DataGridView1.DataError

        e.ThrowException = False

End Sub


Ehaboss  Wednesday, September 30, 2009 8:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Cannot bind dataconnector to empty collection
• Datagrid column heading
• autoincrement pkey in dataset table, table adapter is calling sproc problem
• Cannot access disposed object named 'DataGridTextBox'
• List View Items Back color selection
• typed dataset schema relating to xml file
• Record Begin Edit Event
• How to change(or set) a column to a DataGridViewCheckBoxColumn?
• Add event to the combobox in the multicolumn datagridview.
• Resizing vertically a datagridview component that sits within a horizontally split panel...