Manipulate data in datagridview

davy_g

Well-known member
Joined
Jul 18, 2007
Messages
63
Location
Belgium
Programming Experience
Beginner
Hi,

On my form I have a datagridview in which I have 3 columns, Date, Shift and Hours.
When the user has to fill in this datagridview, the date is already present (id too but it is not visible in the dgv).
When the user gives in a value in the column Shift, I automatically want to assign a value in Column Hours when leaving the column Shift.

So how do I do this? Because it is a datagridview there are no textboxes on which I can set a value based on an event, or can I?

I've searched but could not find anything usefull.
 
Always when you post you find something...

I've used following code

VB.NET:
    Private Sub UrenDataGridView_CellLeave(ByVal sender As Object, ByVal e As 
System.Windows.Forms.DataGridViewCellEventArgs) Handles UrenDataGridView.CellLeave

        If UrenDataGridView.CurrentRow.Cells(2).Value.ToString = "A" Then
            UrenDataGridView.CurrentRow.Cells(3).Value = 7.6
        End If

    End Sub

My values in the specific cell are changed but when I leave my form I get following error on the first if-line:

Object reference not set to an instance of an object.


Why is this caused?
 
Last edited:
Found out that if I click on a textbox after a cellleave and then close my form, I don't get any errormessage.

I know it has something to do with this cellleave event. Does it set a value to NULL or?
What can I do to avoid this error?
 
DataGridView CellLeave event:
Occurs when a cell loses input focus and is no longer the current cell.
So your CurrentRow code will not work here when moving between rows. CellEndEdit or CellValidating events would be better choices.
 
Back
Top