I have written the code
which enables the user to check the checkbox column in datagridview and when the user clicks ok button, the selected rows will be displayed in a msgbox.
Instead of displaying the rows in a msgbox I want to send them to another form (in a listbox).
Public Class Form
WithEvents bsCustomer As New BindingSource
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With {.ColumnName = "Check", .DataType = GetType(Boolean)})
Dim con As New SqlConnection(ConString)
Dim Command = New SqlCommand()
Command.Connection=con
Command.CommandText="SELECT ID, Name FROM Table1"
con.Open()
dt.Load(command.ExecuteReader)
For Each row As DataRow In dt.Rows
row.SetField(Of Boolean)("Check", False)
Next
dt.AcceptChanges()
bsCustomer.DataSource = dt
Datagridview1.DataSource = bsCustomer
End Sub
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim Names =
(
From T In Datagridview1.GetChecked("Check")
Select CStr(T.Cells("name").Value)
).ToArray
If Names.Count > 0 Then
MessageBox.Show(String.Join(Environment.NewLine, Names))
Else
MessageBox.Show("Nothing checked")
End If
End Sub
Public Function GetChecked(ByVal GridView As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnName).Value) = True).ToList
End Function