Imports System.Data.SqlClient
Public Class Form2
Dim conn As New SqlConnection("Data Source=.;Initial Catalog=students;Integrated Security=true")
Dim countcheck As Integer
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.RowIndex < 0 Then Return
Dim ischecked As Boolean = CBool(DataGridView1.Rows(e.RowIndex).Cells(0).Value)
If ischecked Then
countcheck += 1
Else
countcheck -= 1
End If
Label1.Text = countcheck
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cmd As New SqlCommand("Select * from table1", conn)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
Dim checkboxcol As New DataGridViewCheckBoxColumn
checkboxcol.Width = 90
checkboxcol.Name = "checkboxcol"
checkboxcol.HeaderText = "Select student"
DataGridView1.Columns.Insert(0, checkboxcol)
DataGridView1.AllowUserToAddRows = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If countcheck > 0 Then
For Each row As DataGridViewRow In DataGridView1.Rows
Dim select1 As Boolean = Convert.ToBoolean(row.Cells("checkboxcol").Value)
If select1 Then
Dim cmd2 As New SqlCommand("Insert Into table7(firstname,lastname,marks)Values(@firstname,@lastname,@marks)", conn)
cmd2.Parameters.AddWithValue("firstname", row.Cells("firstname").Value)
cmd2.Parameters.AddWithValue("lastname", row.Cells("lastname").Value)
cmd2.Parameters.AddWithValue("marks", row.Cells("marks").Value)
conn.Open()
cmd2.ExecuteNonQuery()
conn.Close()
End If
Next
MessageBox.Show("Data Inserted successfully")
Else
MessageBox.Show("Please select students")
End If
End Sub
End Class