Avatar of javagair
javagair
Flag for United States of America asked on

datagrid update in vb.net

I have a datagrid with 5 columns: date, work detail, work hours, pto detail, pto hours.
when opened it looks at the date and creates a datagrid for every day of the month.
work day automatically get 8 hrs, and weekends get 0 hrs.
work details is a drop down box: here, out office and weekend.
If I change the work details to: out of office for a particular day, I want the hrs to go to 0 and the PTO hours to go to 8.  
If the person chooses non-paid from the PTO detail dropdown PTO hours should also go to zero.
I do not see an event that will be called when something changes on the grid.

Is there a way of doing this without having a commit changes button on the form?

thanks

gary
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
javagair

8/22/2022 - Mon
John (Yiannis) Toutountzoglou

Hi Gary...Please Confirm this is for Windows Form?
for Windows Form DatagridView you may use the EdititngControl Showing Event like:
Private Combo As ComboBox
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If Me.DataGridView1.CurrentRow IsNot Nothing Then
            If Me.DataGridView1.CurrentCell.ColumnIndex = 1 Or Me.DataGridView1.CurrentCell.ColumnIndex = 3 Then
                Combo = CType(e.Control, ComboBox)
                If (Combo IsNot Nothing) Then
                    RemoveHandler Combo.DropDownClosed, New EventHandler(AddressOf Combo_DropDownClosed)
                    AddHandler Combo.DropDownClosed, New EventHandler(AddressOf Combo_DropDownClosed)
                Else
                    Combo = Nothing
                End If
            Else
                Combo = Nothing
            End If
        End If
    End Sub
    Private Sub Combo_DropDownClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.DataGridView1.CurrentRow.Cells(1).IsInEditMode Then
            Dim WorkItem As String = Combo.SelectedItem
            If WorkItem = "out of office" Then
                Me.DataGridView1.CurrentRow.Cells(2).Value = 0
                Me.DataGridView1.CurrentRow.Cells(4).Value = 8
            End If
        ElseIf Me.DataGridView1.CurrentRow.Cells(3).IsInEditMode Then
            Dim PTODetailsItem As String = Combo.SelectedItem
            If PTODetailsItem = "Non Paid" Then
                Me.DataGridView1.CurrentRow.Cells(4).Value = 0
            End If
        End If
    End Sub

Open in new window


If is not a window form application then i am just SORRY!!!!
John
javagair

ASKER
yes it is a form

Private Sub DGV1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellContentClick

this is an event for a datagridview,
I was going to use it to call the code you suggest, but clicking in the cells does not call this event, which I find strange.

suggestions for calling the code?

GARY
Nasir Razzaq

Did you try looking at CellEndEdit event?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
John (Yiannis) Toutountzoglou

You can use also Cell Value Change with CurrentCellDirtyStateChanged Event...
ASKER CERTIFIED SOLUTION
John (Yiannis) Toutountzoglou

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
javagair

ASKER
this is the code i finally used.  Works
 Private Sub DGV1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellValueChanged
       

        Try
            If e.ColumnIndex = DGV1.Columns("DGV1DETAIL").Index Then
                If DGV1.Rows(e.RowIndex).Cells("DGV1DETAIL").Value = "OUT OF OFFICE" Then
                    DGV1.Rows(e.RowIndex).Cells("DGV1HR").Value = 0
                    DGV1.Rows(e.RowIndex).Cells("DGV1PTOHR").Value = 8
                    DGV1.Rows(e.RowIndex).Cells("DGV1PTODET").Value = "PTO"
                Else
                    DGV1.Rows(e.RowIndex).Cells("DGV1DHR").Value = String.Empty
                    DGV1.Rows(e.RowIndex).Cells("DGV1PtoHr").Value = String.Empty
                End If
                DGV1.Invalidate()
            End If
        Catch ex As Exception
        End Try
    End Sub