Hello..

I am developing a windows app and i have a form in which i have 2 datagridviews..

dgv1 is the main gridview with checkbox column and when the user checks the box, that row should be visible in dgv3 (other datagridview)..

The code i used is :

Imports System.Data
Imports System.Data.OleDb
Imports System.EventArgs
Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb.OleDbConnection

Public Class BOM
    Inherits System.Windows.Forms.Form

    Dim wrkdir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location())
    Dim da As New OleDbDataAdapter
    Dim ds As New DataSet
    Dim bs As New BindingSource
    Dim edit As Boolean
    'Dim cnn As OleDbConnection

    Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")

    Private Sub ClearTextBox(ByVal root As Control)

        For Each cntrl As Control In root.Controls
            ClearTextBox(cntrl)


            If TypeOf cntrl Is TextBox Then
                CType(cntrl, TextBox).Text = String.Empty
            End If
        Next cntrl

    End Sub

    Private Sub BOM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'HemDatabase1DataSet3.partno' table. You can move, or remove it, as needed.

        'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet3.partno)
        dgv1.DataSource = Me.HemDatabase1DataSet3.partno
        bs.DataSource = ds.Tables(0)

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        ClearTextBox(Me)
    End Sub

    Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByToolStripButton.Click

        ds.Tables.Clear()

        If TypeToolStripTextBox.Text <> "" Then

            Dim sql As String = "SELECT * from (partno) WHERE type='" & TypeToolStripTextBox.Text & "';"
            Dim cmd As New OleDbCommand(sql, cnn)
            da = New OleDbDataAdapter(cmd)
            da.Fill(ds, "partno")
            bs.DataSource = ds.Tables(0)
            dgv1.DataSource = bs

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ds.Tables.Clear()

        Dim sql As String = "SELECT * From partno;"
        Dim cmd As New OleDbCommand(sql, cnn)

        da.SelectCommand = cmd

        Dim cmdbuilder As New OleDbCommandBuilder(da)
        da.Fill(ds, "partno")
        bs.DataSource = ds.Tables(0)
        dgv1.DataSource = bs

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim f21 As New SelectedBom()
        f21.Show()
    End Sub

    Private Sub dgv1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellContentClick

    End Sub

    Private Sub Btn_Transfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Transfer.Click
        Dim IsRowSelected As Boolean
        Dim SelectedValue As String
        Dim i As Integer

        With Me.dgv3
            '.Rows.Clear()
            For i = 0 To Me.dgv1.RowCount - 1
                IsRowSelected = Me.dgv1.Rows(i).Cells(1).Value
                If IsRowSelected = True Then
                    .Rows.Add(1)
                    SelectedValue = Me.dgv1.Rows(i).Cells(0).Value
                    .Rows(.RowCount - 1).Cells(0).Value = SelectedValue
                End If
            Next

        End With

    End Sub
End Class

I was getting an error on the line :

.Rows.Clear()

saying cannot clear the rows

and then, on commenting it, the error is on line :

.Rows.Add(1)

Please help me as to how should i proceed with this !!

Thank You !!

I am done with most of it.. Just 1 problem now is that i get blank view in datagridview.. Can u please help ?? It shows no data populated !! Code is..

Private Sub Btn_Transfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Transfer.Click
        Dim IsRowSelected As Boolean
        Dim SelectedValue As String
        Dim i As Integer

        With Me.dgv3
            '.Rows.Clear()
            For i = 0 To Me.dgv1.RowCount - 1
                IsRowSelected = Me.dgv1.Rows(i).Cells(1).Value
                If IsRowSelected = True Then
                    .Rows.Add(1)
                    SelectedValue = Me.dgv1.Rows(i).Cells(0).Value
                    .Rows(.RowCount - 1).Cells(0).Value = SelectedValue
                End If
            Next

        End With

    End Sub

Is your 2nd column type of bool? Does it hav true of false values?

No.. none of my columns are of that type !! 1st column is a checkbox column, which i have kept as it is there in main gridview, to equal the number of columns.. But it is hidden.. nonne other columns are of boolean datatype !!

Why do you have this code then:

IsRowSelected = Me.dgv1.Rows(i).Cells(1).Value

This means you have in each row of 2nd column a boolean type of value.

If not, what value do you check there in 2nd column?

nope.. the 2nd column is "partid" which states the id of the part, in database !!

I am attaching screenshots of the gridview as well, to help clear your doubt better..

In the screenshot, the 1st columns in both of them are checkbox columns, however, it is visible in only one grid for selection and hidden in the other one..

I thought so...
So you have to do thia a bit differently to get the rows where the checkBox is added:

For Each row As DataGridViewRow In dataGridView1.Rows
	Dim chk As DataGridViewCheckBoxCell = TryCast(row.Cells(0), DataGridViewCheckBoxCell)
			'this row has a checked checkBox
			'do your code in here...
	If Convert.ToBoolean(chk.Value) = True Then
	End If
Next

I guess this was what I was supposed to do !!

Private Sub Btn_Transfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Transfer.Click
        Dim IsRowSelected As Boolean
        Dim SelectedValue As String
        Dim i As Integer

        For Each row As DataGridViewRow In dgv1.Rows
            Dim chk As DataGridViewCheckBoxCell = TryCast(row.Cells(0), DataGridViewCheckBoxCell)

            With Me.dgv3
                '.Rows.Clear()
                For i = 0 To Me.dgv1.RowCount - 1
                    IsRowSelected = Me.dgv1.Rows(i).Cells(0).Value
                    If IsRowSelected = True Then
                        .Rows.Add(1)
                        SelectedValue = Me.dgv1.Rows(i).Cells(0).Value
                        .Rows(.RowCount - 1).Cells(0).Value = SelectedValue
                    End If
                Next

            End With

            If Convert.ToBoolean(chk.Value) = True Then

            End If

        Next
        
    End Sub

This is giving me blank view of total rows in the database !!

I mean, the output is same as the one attached as a screenshot above !! Only, the rows visible in the blank gridview have increased in number, equal to the entries in database !! :(

Why cant you give button in form, let the user make selection and click button to show records on form 2. And use the view to filter the records. This is the second thread u opend for same issue. :) try different ways of doing things. If you keep doing the same thing result will be the same.. :)

The option u are suggesting now is what i had tried before.. It didn't work and so i switched to this method..

I tried to debug and what I could conclude was that the rows with checkbox are read but there is a difficulty in fetching the data and displaying it in the other grid..

The code I use is :

Private Sub Btn_Transfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Transfer.Click
        Dim IsRowSelected As Boolean
        Dim SelectedValue As String
        Dim i As Integer

        'For Each row As DataGridViewRow In dgv1.Rows
        'Dim chk As DataGridViewCheckBoxCell = TryCast(row.Cells(0), DataGridViewCheckBoxCell)

        With Me.dgv3
            '.Rows.Clear()
            For i = 0 To Me.dgv1.RowCount - 1
                IsRowSelected = Me.dgv1.Rows(i).Cells(0).Value
                If IsRowSelected = True Then
                    .Rows.Add(1)
                    SelectedValue = Me.dgv1.Rows(i).Cells(0).Value
                    .Rows(.RowCount - 1).Cells(0).Value = SelectedValue
                End If
            Next

        End With

        'If Convert.ToBoolean(chk.Value) = True Then

        'End If

        'Next
        
    End Sub

Here, the problem seems to be somewhere in

SelectedValue = Me.dgv1.Rows(i).Cells(0).Value

as this evaluates to the value "Nothing"..

I am also attaching the output I m obtaining..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.