-1

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mib-af
  • 3
  • 1
  • 4

1 Answers1

0

You have to know the index of the checkbox-column, then you can use this:

IEnumerable<DataGridViewRow> GetCheckedRows(DataGridView grid, int checkboxColumnIndex)
{
    List<DataGridViewRow> checkedRows = new List<DataGridViewRow>();
    for (int rowIndex = 0; rowIndex < grid.RowCount; rowIndex++)
    {
        var chkCell = grid[checkboxColumnIndex, rowIndex] as DataGridViewCheckBoxCell;
        bool isChecked = (bool)chkCell.EditedFormattedValue;
        if (isChecked)
            checkedRows.Add(grid.Rows[rowIndex]);
    }
    return checkedRows;
}

Now you have to have an instance of Form2 to be able to pass these rows to it, for example via a public property or method. Instead of passing DataGridViewRow it would be better to pass the selected objects. But since you haven't mentioned the datasource i have used the rows.


VB.NET:

Function GetCheckedRows(grid As DataGridView, checkboxColumnIndex As Integer) As IEnumerable(Of DataGridViewRow)
    Dim checkedRows As New List(Of DataGridViewRow)
    For rowIndex As Integer = 0 To grid.RowCount - 1
        Dim chkCell = TryCast(grid(checkboxColumnIndex, rowIndex), DataGridViewCheckBoxCell)
        Dim isChecked As Boolean = CBool(chkCell.EditedFormattedValue)
        If isChecked Then
            checkedRows.Add(grid.Rows(rowIndex))
        End If
    Next
    Return checkedRows
End Function
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • i m using VB.net @Tim schmelter – Mib-af Oct 28 '15 at 15:18
  • plz have a look @Tim Schmelter – Mib-af Oct 28 '15 at 20:47
  • @Mib-af: What's not working? Where's the instance of the other form? You know how to pass objects from one form to another? There are plenty of questions on SO asking the same, like [here](http://stackoverflow.com/questions/4887820/how-do-you-pass-an-object-from-form1-to-form2-and-back-to-form1) or [here](http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) or [here](http://stackoverflow.com/questions/4373660/communicating-between-forms) or [here](http://stackoverflow.com/questions/3034493/communication-between-forms-in-c-sharp). – Tim Schmelter Oct 29 '15 at 08:35
  • i m not using objects . is it necessary to use objects ? @Tim Schmelter – Mib-af Oct 29 '15 at 12:23
  • @Mib-af: everything is an object, what do you want to pass? You could use a constructor: `Dim checkedRows=GetChecked("Check") Dim otherForm=new Form2(checkedRows)` or a property: `otherForm.Rows=checkedRows` (you have to provide that property `Rows` yourself, it must be public). – Tim Schmelter Oct 29 '15 at 12:42
  • plz give me ur email ..... i will send the files . plz dont ignore @tim schmelter – Mib-af Oct 29 '15 at 12:51