For me to be able to select all the row in datagridview

做~自己de王妃 提交于 2019-12-13 04:23:56

问题


For me to be able to select all rows in my datagrid and after i select all the row the next step im going to do is to save all the rows i have selected in other table through database now im using a checkbox to select all the rows i have the code for checking all the checkbox my problem is the saving to save all the rows that ive checked in just one button

 Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As                   System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim countCheckDatas As Integer = 0
If e.RowIndex = -1 Then
    '----------Check if all datagridview columns as checked----------
    For count = 0 To DataGridView1.RowCount - 1
        If DataGridView1.Rows(count).Cells(e.ColumnIndex).Value = True Then
            countCheckDatas += 1
        End If
    Next
    If countCheckDatas <> DataGridView1.RowCount Then
        If MsgBox("Do you want to check all checkbox ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Check all") = MsgBoxResult.Yes Then
            For DGVCols = 0 To DataGridView1.RowCount - 1
                DataGridView1.Rows(DGVCols).Cells(e.ColumnIndex).Value = True
            Next
        End If
    Else
        If MsgBox("Do you want to uncheck all checkbox ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Uncheck all") = MsgBoxResult.Yes Then
            For DGVCols = 0 To DataGridView1.RowCount - 1
                DataGridView1.Rows(DGVCols).Cells(e.ColumnIndex).Value = False
            Next
        End If
    End If
End If
End Sub

回答1:


Hi Jhen i just finish the code, just replace this code into your button to store all the data and display it on a new datagridview...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        For i = 0 To DataGridView1.RowCount - 1
            If DataGridView1.Rows(i).Cells(1).Value = True Then
                txtattendance.Text = DataGridView1.Item(2, i).Value
                txtnames.Text = DataGridView1.Item(3, i).Value
                txtsection.Text = DataGridView1.Item(4, i).Value
                txtclasssubject.Text = DataGridView1.Item(5, i).Value
                txttimein.Text = DataGridView1.Item(6, i).Value
                txtinstructorname.Text = DataGridView1.Item(7, i).Value
                txtclasstime.Text = DataGridView1.Item(8, i).Value
                txttimeout.Text = TimeOfDay
                Me.DataGridView2.Rows.Add(Me.txtattendance.Text, Me.txtnames.Text, Me.txtsection.Text, Me.txtclasssubject.Text, Me.txttimein.Text, Me.txttimeout.Text, Me.txtinstructorname.Text, Me.txtclasstime.Text)
                myconnection.Open()
                'Declaration of Variables
                Dim str As String
                Dim vAttendance As String
                Dim vNames As String
                Dim vSection As String
                Dim vClassSubject As String
                Dim vTimeIn As String
                Dim vTimeOut As String
                Dim vInstructorName As String
                Dim vClassTime As String
                vAttendance = txtattendance.Text
                vNames = txtnames.Text
                vSection = txtsection.Text
                vClassSubject = txtclasssubject.Text
                vTimeIn = txttimein.Text
                vTimeOut = txttimeout.Text
                vInstructorName = txtinstructorname.Text
                vClassTime = txtclasstime.Text
                str = "Insert into DATA ([Attendance],[Names],[Section],[ClassSubject],[TimeIn],[TimeOut],[InstructorName],[ClassTime]) values (?,?,?,?,?,?,?,?)"
                Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
                cmd.Parameters.AddWithValue("@Attendance", vAttendance)
                cmd.Parameters.AddWithValue("@Names", vNames)
                cmd.Parameters.AddWithValue("@Section", vSection)
                cmd.Parameters.AddWithValue("@ClassSubject", vClassSubject)
                cmd.Parameters.AddWithValue("@TimeIn", vTimeIn)
                cmd.Parameters.AddWithValue("@TimeOut", vTimeOut)
                cmd.Parameters.AddWithValue("@InstructorName", vInstructorName)
                cmd.Parameters.AddWithValue("@ClassTime", vClassTime)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                myconnection.Close()
            End If
        Next
        MessageBox.Show("You Just Had Time Out , On Your Class!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As OleDb.OleDbException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "oledb Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
End Sub

And Also Change The DataGridView1 Code To This...

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim countCheckDatas As Integer = 0
    If e.RowIndex = -1 And e.ColumnIndex = 1 Then
        '----------Check if all datagridview columns as checked----------
        For count = 0 To DataGridView1.RowCount - 1
            If DataGridView1.Rows(count).Cells(e.ColumnIndex).Value = True Then
                countCheckDatas += 1
            End If
        Next
        If countCheckDatas <> DataGridView1.RowCount Then
            If MsgBox("Do you want to check all checkbox ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Check all") = MsgBoxResult.Yes Then
                For DGVCols = 0 To DataGridView1.RowCount - 1
                    DataGridView1.Rows(DGVCols).Cells(e.ColumnIndex).Value = True
                Next
            End If
        Else
            If MsgBox("Do you want to uncheck all checkbox ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Uncheck all") = MsgBoxResult.Yes Then
                For DGVCols = 0 To DataGridView1.RowCount - 1
                    DataGridView1.Rows(DGVCols).Cells(e.ColumnIndex).Value = False
                Next
            End If
        End If
    End If
End Sub

one more thing,

I Saw the Connection String to the MS access but the location is not valid, the MS Access DB is located in your Project folder...

provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'DataFile = "E:\Mytime\TIMEandOUT\TIMEandOUT\bin\Debug\BackUp\Testing.accdb"
DataFile = My.Application.Info.DirectoryPath.ToString() & "\BackUp\testing.Accdb;Persist Security Info=False;"

Best Regards,



来源:https://stackoverflow.com/questions/24029499/for-me-to-be-able-to-select-all-the-row-in-datagridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!