dgv... not playing nice

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
dgv will not delete and move to next record more than once while form is open. I can open the form, delete a record, and it will move to the next record if there is one. If I then try to delete that record, it deletes the record but does not move to the next available record.

If I close the form and reopen it, the proper data is displayed and I can repeat the process.

Hoe do I get it to refresh the state after the first delete?
 
Last edited:
That is still not refreshing the dgv. I am using the UserDeletingRow event to handle the delete. At the end of doing the delete I am calling Sub1(). Sub1() is the sub that actually get the datasource for, and binds, the dgv.

commented out the call to the sub, and replaced it with the dgv.ds = dt1, dgv.refresh() and it still does not show the new set of data. I will post my code in the post below this one.
 
I will post my code in the post below this one.

VB.NET:
Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
    If (Not e.Row.IsNewRow) Then
      Dim response As DialogResult = MessageBox.Show("Are you sure you want to delete this row?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
      If (response = DialogResult.No) Then
        e.Cancel = True
      End If
      _Conn = New SqlConnection(_ConnString)
      Try
        Dim theRow As Integer = DataGridView1.CurrentRow.Index
        Dim gridRow As DataGridViewRow = DataGridView1.Rows(theRow)
        Dim depositID As Integer = 0
        If gridRow.Cells(0).EditedFormattedValue.ToString().Length() > 0 Then
          depositID = Int32.Parse(gridRow.Cells(0).EditedFormattedValue.ToString())
        End If

        Dim DelCommandString As String = "usp_Deposit_Delete"
        Dim cmdDEL As New SqlCommand(DelCommandString, _Conn)

        cmdDEL.CommandType = CommandType.StoredProcedure

        Dim myUtil As New Utils
        With myUtil
          .AddParamToSQLCmd(cmdDEL, "@DepositID", SqlDbType.Int, 4, ParameterDirection.Input, depositID)
        End With

        _da.DeleteCommand = cmdDEL

        _Conn.Open()
        cmdDEL.ExecuteNonQuery()
        '_da.Update(_dsGrid.Tables("OneDay"))
        '_dsGrid.AcceptChanges()
        _Conn.Close()

      Catch ex As Exception
        MessageBox.Show("Error in Deleting Data: " & ex.Message & vbCrLf & vbCrLf & ex.StackTrace)
      Finally
        _Conn.Dispose()
      End Try
      updateBindingSource()
      If BindingNavigator1.BindingSource.Count < 0 Then
        'getDepositsForSelectedDate()
        DataGridView1.DataSource = _dsGrid.Tables("OneDay")
        DataGridView1.Refresh()

      End If

    End If
  End Sub

VB.NET:
Private Sub getDepositsForSelectedDate()

    If IsDate(DateTimePicker1.Text) Then
      'If IsDate(txtTime.Text) Then
      Dim theDate As Date = Date.Parse(DateTimePicker1.Text)
      'Dim theDate As Date = Date.Parse(txtTime.Text)
      Dim sql As String = ""
      sql += "exec usp_Deposit_GetBy_DateRange "
      sql += "@StartDate = '" & theDate.ToShortDateString() & "', "
      sql += "@EndDate = '" & theDate.ToShortDateString() & "' "

      _Conn = New SqlConnection(_ConnString)
      Try
        _da = New SqlDataAdapter(New SqlCommand(sql, _Conn))
        _dsGrid = New DataSet("TodaysDeposits")
        _dsGrid.Tables.Add("OneDay")
        _da.Fill(_dsGrid.Tables("OneDay"))
        _isBinding = True
        DataGridView1.DataSource = _dsGrid.Tables("OneDay")
        _isBinding = False
      Catch ex As Exception
        MessageBox.Show("ERROR in getDepositsForSelectedDate(): " & ex.Message & vbCrLf & vbCrLf & ex.StackTrace)
      Finally
        _Conn.Dispose()
      End Try
    End If
    formatGrid()
    calcTotals()
  End Sub
 
VB.NET:
If BindingNavigator1.BindingSource.Count < 0 Then
        'getDepositsForSelectedDate()
        DataGridView1.DataSource = _dsGrid.Tables("OneDay")
        DataGridView1.Refresh()

This is all happening before you've actually deleted the record. Try the DataGridView1_UserDeletedRow event and see if you experience any different behavior
 
Actually that is happening after the it is deleted. The cmdDEL.ExecuteNonQuery() is running well before this happens.

Like I said, the record is deleting from the DB, the dgv is just not refreshing with the new dataset.
 

Latest posts

Back
Top