CHECKBOX所选项目想要添加

CHECKBOX所选项目想要添加

本文介绍了DGV CHECKBOX所选项目想要添加。如果有数据库需要更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用DataGridView First列Checkbox,Regno,Name Like。当Checkbox为True表示我想添加新记录。如果有意味着我需要更新



这里我使用Code这段代码正在运行新的Recored它不会更新..



请告诉您有价值的建议。

Hi all, I am using a DataGridView First column Checkbox, Regno, Name Like . When Checkbox is True means i Want To Add New Record. if have means i need to UPDATE

Here I use Code this code is working New Recored it not going to UPDATE..

Please Tell your Valuable Advice .

Dim strda1 As SqlDataReader
        For Each row As DataGridViewRow In DGV1_SA.Rows
            Dim cell As DataGridViewCheckBoxCell = row.Cells(0)

            For ix As Integer = 0 To DGV1_SA.SelectedRows.Count - 1
                Dim strSQL12 As String = "select * from Attendance WHERE Regno='" & DGV1_SA.Rows(ix).Cells(1).Value & "' and On_Date='" & CDate(DTP1_SA.Value) & "'"
                Me.DaAp12 = New SqlDataAdapter(strSQL12, con)
                Dim Dset12 As New DataSet
                DaAp12.Fill(Dset12)

                If cell.Value = True Then
                    ' ADD NEW RECORED 
                    If strSQL12 <> "" Then
                        con.Open()
                        Dim VRegNo As String = row.Cells("Regno").Value
                        Dim VS_Name As String = row.Cells("sname").Value
                        cmd.CommandText = "INSERT INTO Attendance (Regno,Stud_Name,On_Date,I,II,III,IV,V,VI,VII,VIII,IX,X) VALUES ('" & VRegNo & "','" & VS_Name & "'," & _
                            "'" & CDate(DTP1_SA.Value) & "','" & CB1.Checked & "','" & CB2.Checked & "','" & CB3.Checked & "','" & CB4.Checked & "','" & CB5.Checked & "', " & _
                            "'" & CB6.Checked & "','" & CB7.Checked & "','" & CB8.Checked & "','" & CB9.Checked & "','" & CB10.Checked & "')"

                        cmd.ExecuteNonQuery()
                        con.Close()

                    Else ' UPDATE 
                        con.Open()
                        cmd.CommandText = ("UPDATE Attendance SET I='" & CB1.Checked & "',II='" & CB2.Checked & "',III='" & CB3.Checked & "', IV='" & CB4.Checked & "', V='" & CB5.Checked & "',  " & _
                                          "  VI='" & CB6.Checked & "' , VII='" & CB7.Checked & "' , VIII='" & CB8.Checked & "' , IX='" & CB9.Checked & "' , X='" & CB10.Checked & "' " & _
                                           " WHERE Regno='" & DGV1_SA.Rows(ix).Cells(1).Value & "' and On_Date='" & CDate(DTP1_SA.Value) & "'")

                        strda1 = cmd.ExecuteReader()
                        con.Close()
                    End If
                End If
            Next ix
        Next row
        MessageBox.Show("UpDate")



已添加代码块[/ Edit]


Code block added[/Edit]

推荐答案

If strSQL12 <> "" And Dset12.Tables(0).Rows.Count = 0 Then





首先,strSQL12永远不会是空的那你为什么要检查呢?你将它设置在上面的几行,似乎永远不会清除它。



其次,你的Dset12在循环通过行之前设置。您已将数据放入此表中,该表仅匹配网格中的第一个SELECTED行。它可能是也可能不是选中复选框的行。这不是你正在循环中看到的行。



你需要在循环内检查表中是否存在该记录或不。



First of all, strSQL12 is never going to be empty, so why are you checking it? You set it a few lines above and never seem to clear it out.

Second, your Dset12 is set before your loop through the rows. You''ve put data into this table that ONLY matches the first SELECTED row in the grid. It may or may not be a row that has a checkbox checked. It isn''t the row that you are currently looking at in your loop.

You need to check, within the loop, if that record exists in the table already or not.


For i As Integer = 0 To DataGridView1.Rows.Count - 1
            '==========================================1=====================

            Dim strSQL12 As String = "select * from AttendanceD WHERE Regno='" & DataGridView1.Rows(i).Cells(1).Value & "' and On_Date='" & CDate(DTP1.Value) & "'"
            Me.DaAp12 = New SqlDataAdapter(strSQL12, con)
            Dim Dset12 As New DataTable
            DaAp12.Fill(Dset12)
            DataGridView2.DataSource = Dset12
            Dset12.AcceptChanges()
            '=================================================================
            Dim ch1 As New DataGridViewCheckBoxCell()
            ch1 = CType(DataGridView1.Rows(DataGridView1.Rows(i).Index).Cells(0), DataGridViewCheckBoxCell)

            Select Case ch1.Value
                Case "True"
                    If strSQL12 <> "" AndAlso Dset12.Rows.Count - 1 Then '1
                        con.Open()
                        Dim VRegNo As String = DataGridView1.Rows(i).Cells("Regno").Value
                        Dim VS_Name As String = DataGridView1.Rows(i).Cells("Sname").Value
                        cmd.CommandText = "INSERT INTO AttendanceD (Regno,SName,On_Date,I,II,III) VALUES ('" & VRegNo & "','" & VS_Name & "'," & _
                            "'" & CDate(DTP1.Value) & "','" & CB1.Checked & "','" & CB2.Checked & "','" & CB3.Checked & "')"
                        cmd.ExecuteNonQuery()
                        con.Close()
                    Else
                        con.Open()
                        cmd.CommandText = ("UPDATE AttendanceD SET I='" & CB1.Checked & "',II='" & CB2.Checked & "',III='" & CB3.Checked & "' " & _
                                           " WHERE Regno='" & DataGridView1.Rows(i).Cells(1).Value & "' and On_Date='" & CDate(DTP1.Value) & "'")
                        strda1 = cmd.ExecuteReader()
                        con.Close()
                    End If '1
                    ' MessageBox.Show("Add")
                Case "False"
                    '  ch1.Value = True
                    MessageBox.Show("U?pdate")
            End Select

        Next i
        MessageBox.Show("Add")


这篇关于DGV CHECKBOX所选项目想要添加。如果有数据库需要更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:11