I am developing a Windows Forms Based Application using dbo.database and Visual Basic:

I populate some Orders in DataGridView1 using LINQ 2 SQL from "tbl_Orders" where OrderStatus is "Pending". I change OrderStatus Column Type to "CheckBox" in DataGridView, Now I want that when I select some records (Orders) by checking respective checkboxes and click on "btn_UpdateStatus" Button, program pick OrderID's of only selected orders and update OrderStatus of Only selected Records (Orders) from "Pending" to "Completed" in Database Table "tbl_Orders" and DataGridView1 should be Refreshed.

I am using VB, Kindly help me with complete solution?

Code Im using is:

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim ID As Integer = DirectCast(DataGridViewIU68E.SelectedRows(0).Cells("EvaluationIDDataGridViewTextBoxColumn").Value, Integer)
        For i = 1 To DataGridViewIU68E.SelectedRows.Count
            Dim db As New IU68IL33EvaluationDataContext

            Dim ee = From u In db.IU68_IL33_Evaluations _
                     Where u.EvaluationID = ID _
                     Select u
            ee.EmailStatus = "Done"
            db.SubmitChanges()
        Next
    End Sub

Dani AI

Generated

is correct: SelectedRows is for user-selected rows (click/shift/ctrl), not for checkbox state. The pattern that works reliably is (1) let the grid have a dedicated checkbox column for selection (e.g. "Selected"), (2) collect the OrderID values from rows where that checkbox is checked, (3) create a single LINQ-to-SQL DataContext, fetch the matching orders, update them, then call SubmitChanges(), and (4) rebind the grid from a fresh DataContext. Avoid creating a new DataContext inside the loop or trying to update the DB from each row individually.

Example (adjust column and context names to match your project):

Private Sub btn_UpdateStatus_Click(sender As Object, e As EventArgs) Handles btn_UpdateStatus.Click
    Dim ids As New List(Of Integer)
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.IsNewRow Then Continue For
        Dim chk = row.Cells("Selected").Value
        Dim checked As Boolean = False
        If chk IsNot Nothing AndAlso chk IsNot DBNull.Value Then Boolean.TryParse(chk.ToString(), checked)
        If checked Then
            Dim idVal = row.Cells("OrderID").Value
            Dim id As Integer
            If Integer.TryParse(Convert.ToString(idVal), id) Then ids.Add(id)
        End If
    Next

    If ids.Count = 0 Then Return
    Using db As New OrdersDataContext()
        Dim orders = (From o In db.tbl_Orders Where ids.Contains(o.OrderID)).ToList()
        For Each o In orders
            o.OrderStatus = "Completed"
        Next
        db.SubmitChanges()
    End Using

    ' Rebind from a fresh context so grid reflects DB state
    Using db As New OrdersDataContext()
        DataGridView1.DataSource = (From o In db.tbl_Orders Where o.OrderStatus = "Pending").ToList()
    End Using
End Sub

Troubleshooting: call BindingSource.EndEdit() before reading values if you use a BindingSource; check for DBNull on checkbox cells; use a separate "Selected" column rather than reusing the bound OrderStatus column as a checkbox (keeps UI selection separate from the persisted value).

I may be wrong, but SelectedRows won't do it for you. SelectedRows is used for rows selected (ie clicked on or multi-selected with Shift or Control pressed while clicking) and not for rows that have been "selected" with a checkbox.

I believe you need to change SelectedRows to Rows and check the value of the checkbox. If the checkbox is checked then do your thing, if not move to the next record.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.