让我能够选择 datagridview 中的所有行 [英] For me to be able to select all the row in datagridview
问题描述
为了能够选择数据网格中的所有行,在选择所有行后,我要做的下一步是通过数据库保存我在其他表中选择的所有行,现在我使用复选框进行选择所有行我都有检查所有复选框的代码我的问题是保存我只用一个按钮检查过的所有行
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
推荐答案
我刚刚完成代码,只需将此代码替换到您的按钮中即可存储所有数据并将其显示在新的 datagridview 上...
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
并将 DataGridView1 代码更改为此...
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
还有一件事,
我看到了 MS 访问的连接字符串,但位置无效,MS Access DB 位于您的项目文件夹中...
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;"
此致,
这篇关于让我能够选择 datagridview 中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!