简体   繁体   English

如何使用复选框更新特定行?

[英]How to update specific row using checkbox?

This is what I've tried. 这就是我尝试过的。 This works for checkbox value only and all displayed data will be updated. 这仅适用于复选框值,并且所有显示的数据都会更新。

 Public Sub updateDGV()



    Dim id As String
    Dim cb As String
    Dim time As String
    Dim str As String
    Dim mycon As New SqlConnection(ConString)
    Try
        mycon.Open()
        For Each row As DataGridViewRow In dgv.Rows
            row.Cells(22).Value = Date.Now.ToString("MM/dd/yyyy HH:mm:ss tt")
            id = row.Cells(0).Value.ToString
            cb = row.Cells(1).Value.ToString
            time = row.Cells(22).Value.ToString
            str = "UPDATE GuardMonitoring SET EmploymentStatus =@EmpStat, 
           ModifiedBy =@ModifiedBy, ModifyDate =@ModifyDate WHERE 
            (Employee_Id =@EmpId)"


            Dim cmd As SqlCommand = New SqlCommand(str, mycon)
            cmd.Parameters.AddWithValue("@EmpId", SqlDbType.VarChar).Value = id
            cmd.Parameters.AddWithValue("@EmpStat", SqlDbType.VarChar).Value = cb
            cmd.Parameters.AddWithValue("@ModifiedBy", SqlDbType.VarChar).Value = UserPass.txtfull2.Text
            cmd.Parameters.AddWithValue("@ModifyDate", SqlDbType.VarChar).Value = time
            cmd.ExecuteNonQuery()

        Next
        mycon.Close()
    Catch ex As Exception
        showmsg.ForeColor = Color.LightCoral
        showmsg.Text = "Rows not found!"
        MsgBox(ex.Message)
    End Try

End Sub

Expected output: update only when changing the specific value of checkbox. 预期输出:仅在更改复选框的特定值时更新。

Do you mean that you want to update only the row that have checkboxes turned ON? 您是说只想更新复选框处于打开状态的行吗? If so, it can be determined by the value of the checkbox cell. 如果是这样,则可以通过复选框单元格的值来确定。

For example, like this. 例如,像这样。

 Public Sub updateDGV()



    Dim id As String
    Dim cb As String
    Dim time As String
    Dim str As String
    Dim mycon As New SqlConnection(ConString)
    Try
        mycon.Open()
        For Each row As DataGridViewRow In dgv.Rows
            If Convert.ToBoolean(row.Cells("your checkbox column name").Value) Then
                row.Cells(22).Value = Date.Now.ToString("MM/dd/yyyy HH:mm:ss tt")
                id = row.Cells(0).Value.ToString
                cb = row.Cells(1).Value.ToString
                time = row.Cells(22).Value.ToString
                str = "UPDATE GuardMonitoring SET EmploymentStatus =@EmpStat, 
               ModifiedBy =@ModifiedBy, ModifyDate =@ModifyDate WHERE 
                (Employee_Id =@EmpId)"


                Dim cmd As SqlCommand = New SqlCommand(str, mycon)
                cmd.Parameters.AddWithValue("@EmpId", SqlDbType.VarChar).Value = id
                cmd.Parameters.AddWithValue("@EmpStat", SqlDbType.VarChar).Value = cb
                cmd.Parameters.AddWithValue("@ModifiedBy", SqlDbType.VarChar).Value = UserPass.txtfull2.Text
                cmd.Parameters.AddWithValue("@ModifyDate", SqlDbType.VarChar).Value = time
                cmd.ExecuteNonQuery()
            End If

        Next
        mycon.Close()
    Catch ex As Exception
        showmsg.ForeColor = Color.LightCoral
        showmsg.Text = "Rows not found!"
        MsgBox(ex.Message)
    End Try

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM