locked
DataGridView - column does not allow null values (Prevent row being automatically removed)

    Question

  • Hi

    I have a databound DataGridView and use a button to control updates.

    The problem is that if a db Column does not allow a null value an error is thrown ("Column <ID> does not allow null values") and then the datagridview automatically removes the row.  This error is raised as soon as the user leaves the cell and before the update command is issued.  Very frustrating as the user has to reinput all the data. 

    I've looked online for help but can't seem to find a reasonable solution.   Any ideas on how to prevent this from happening?  Ideally, I could handle this error in the same way I handle "logic" errors - set the error icon and leave the row as it is.

    I don't want to use default values as this isn't always possible.

    Help is gratefully received.

    Gordon
    Thursday, July 30, 2009 11:50 AM

Answers

  • Hi GWILSON1981,

    Do you use the wizard to add datasource for datagirdview?

    For cellvalidating event, if you set e.Cancel = True, you won't be able to leave the cell until you input value.
    For example, column 1 (index form 0) is not allowed null in your datagridview. You can set like this:

        Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            If e.ColumnIndex = 1 And e.FormattedValue.ToString = "" Then
                DataGridView1.Rows(e.RowIndex).ErrorText = _
                   "Please input a value."
                e.Cancel = True
            End If
        End Sub


    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

    Best Regards

    Yichun Feng

     

    • Marked as answer by Yichun_Feng Wednesday, August 05, 2009 2:47 AM
    Monday, August 03, 2009 5:00 AM

All replies

  • I haven't actually tested this so I don't know if it will work, but how about something like this:

    Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, _
    	ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
    	Handles DataGridView1.CellValidating
    
    	If CType(sender, DataGridViewCell).Value Is Nothing Then e.Cancel = True
    End Sub
    
    Thursday, July 30, 2009 12:13 PM
  • I haven't actually tested this so I don't know if it will work, but how about something like this:

    Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, _
    
    	ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
    
    	Handles DataGridView1.CellValidating
    
    
    
    	If CType(sender, DataGridViewCell).Value Is Nothing Then e.Cancel = True
    
    End Sub
    
    

    Hi
    I have tried something like that already and it doesn't seem to make any difference!  The datagridview just continues to remove the erroneous row.

    Thanks for trying to help me though :-)
    Thursday, July 30, 2009 12:29 PM
  • Hi GWILSON1981,

    Do you use the wizard to add datasource for datagirdview?

    For cellvalidating event, if you set e.Cancel = True, you won't be able to leave the cell until you input value.
    For example, column 1 (index form 0) is not allowed null in your datagridview. You can set like this:

        Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            If e.ColumnIndex = 1 And e.FormattedValue.ToString = "" Then
                DataGridView1.Rows(e.RowIndex).ErrorText = _
                   "Please input a value."
                e.Cancel = True
            End If
        End Sub


    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

    Best Regards

    Yichun Feng

     

    • Marked as answer by Yichun_Feng Wednesday, August 05, 2009 2:47 AM
    Monday, August 03, 2009 5:00 AM