Results 1 to 13 of 13

Thread: [RESOLVED] Datagridview looping issue?

Threaded View

  1. #1

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Resolved [RESOLVED] Datagridview looping issue?

    I have this Datagridview that has a Checkbox column. I also have a button to update the database. What I want to accomplish is to loop all rows that has a checkbox column value will be submitted to the database for updates. The problem is it will only update 1 row at a time. It does not loop to all rows. I tried different techniques to no avail. The UpdateRecord() is just a simple code to update a Boolean field to true or false.


    I also have a combobox with list items (Uncleared, Cleared)


    Version(1)
    Code:
            
    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
    
    Dim counter As Integer = 0
    
    
    
            If Me.cboisCleared.SelectedIndex = 0 Then  -- (0 = Uncleared) code to update all uncleared to true.
                For counter = 1 To dgvDisbursements.RowCount
                    If dgvDisbursements.CurrentRow.Cells("isCleared").Value = True Then
                        UpdateRecord()
                    End If
                Next
            Else (1 = Cleared) --code to update all cleared to false.
                For counter = 1 To dgvDisbursements.RowCount
                    If dgvDisbursements.CurrentRow.Cells("isCleared").Value = False Then
                        UpdateRecord()
                    End If
                Next
            End If
    
    end Sub

    Version(2)
    Code:
    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
    
            If Me.cboisCleared.SelectedIndex = 0 Then --(0 = Uncleared) code to update all uncleared to true.
                For Each row As DataGridViewRow In dgvDisbursements.Rows
                    If row.Cells("isCleared").Value = True Then
                        UpdateRecord()
                    End If
                Next
            Else --(1 = Cleared) code to update all cleared to False.
                For Each row As DataGridViewRow In dgvDisbursements.Rows
                    If row.Cells("isCleared").Value = False Then
                        UpdateRecord()
                    End If
                Next
            End If
    
    End Sub
    Version 3
    Code:
    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
    
            If cboisCleared.SelectedIndex = 0 Then  --(0 = Uncleared) code to update all uncleared to true.
                For Each row As DataGridViewRow In dgvDisbursements.Rows
                    If Boolean.TryParse(row.Cells("isCleared").Value.ToString, isTrue) Then
                        If isTrue = True Then
                            UpdateRecord()
                        End If
                    End If
                Next
            Else -- --(1 = Cleared) code to update all cleared to False.
                For Each row As DataGridViewRow In dgvDisbursements.Rows
                    If Boolean.TryParse(row.Cells("isCleared").Value.ToString, isTrue) Then
                        If isTrue = False Then
                            UpdateRecord()
                        End If
                    End If
                Next
            End If
    
    End sub

    Code:
        Private Sub UpdateRecord()
    
    
            Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString
            Dim connection As SqlConnection = New SqlConnection(CS)
            Dim updateStatment As String = "sproc_tblDisbursements_isCleared_DateCleared_Update"
            Dim updateCommand As New SqlCommand(updateStatment, connection)
            updateCommand.CommandType = CommandType.StoredProcedure
            updateCommand.Parameters.AddWithValue("@DisbursementID", CLng(dgvDisbursements.CurrentRow.Cells("DisbursementID").Value))
            updateCommand.Parameters.AddWithValue("@isCleared", CBool(dgvDisbursements.CurrentRow.Cells("isCleared").Value))
            updateCommand.Parameters.AddWithValue("@DateCleared", CDate(dgvDisbursements.CurrentRow.Cells("DateCleared").Value))
            updateCommand.Parameters.AddWithValue("@UserID", g_UserID)
            Try
                connection.Open()
                updateCommand.ExecuteNonQuery()
            Catch ex As SqlException
                Throw ex
            Finally
                connection.Close()
            End Try
    
        End Sub

    I want to loop to all records and update the database. All of the above codes will only update 1 row at a time. What am I doing wrong?
    Last edited by coolwater; Nov 23rd, 2013 at 03:31 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width