Hello !!

Being a newbie for vb.net, I seek your advice in a windows application I am developing.

I have a form consisting of datagridview with as checkbox column. Now, when the user selects a row using checkbox, I want a new form to pop-up and the selected rows be displayed on that form..

The rows in the table consist of quantity and cost of materials, so i also want that the total quantity be displayed in a textbox on the other form..

Could anybody please suggest as to what should i use on the other form, as in, textbox, datagridview, table etc.

Also do let me know how should i go about it as i am completely new and finding it very tough to implement. Searching since two days but no idea of how and what to write.

Please Help !!

Thank You..

Recommended Answers

All 33 Replies

In your second form Write public sub which takes datatable or dataset as parameter.
When user selects the rows by checking the checkbox and clicks the button, instanitate the object of form 2 and prepare and pass ur datatable or dataset. And use it to show in form 2.

I am very new so no clue what to write and how to write.. Please help..

Can you post your code of form1 which loads or binds data to grid?

This is the code i have used for the project on first form..

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

Public Class Form1
    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;")

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

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        Dim change As DataSet = HemDatabase1DataSet.GetChanges()
        Dim commandbuild As New OleDbCommandBuilder(da)
        da.SelectCommand = New OleDbCommand("SELECT * From partno", cnn)

        If change IsNot Nothing Then
            da.Update(change, "partno")
        End If

        'dgv1.DataSource = bs
        'If edit Then
        '    da.Update(ds, "partno")
        '    edit = False
        'End If

    End Sub

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

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

        'dgv1.DataMember = "partno"

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

    End Sub

    Private Sub button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.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

        'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet.partno)
        'dgv1.DataSource = Me.HemDatabase1DataSet.partno
        'ds.Tables.Clear()

        'If tsText.Text <> "" Then

        '    Dim sql As String = "SELECT partnum, partname, partdesc, partqty " & _
        '                        "FROM (partno) " & _
        '                        "WHERE type='" & tsText.Text & "';"

        '    Dim cmd As New OleDbCommand(sql, cnn)
        '    da = New OleDbDataAdapter(cmd)

        '    'da.SelectCommand = cmd
        '    'Dim cmdBuilder As New OleDbCommandBuilder(da)

        '    da.Fill(ds, "partno")
        '    bs.DataSource = ds.Tables(0)

        'Else

        '    Exit Sub

        'End If
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles button3.Click

        ds.Tables.Clear()

        If tsText.Text <> "" Then

            Dim sql As String = "SELECT * from (partno) WHERE type='" & tsText.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


        'Try
        '    Me.PartnoTableAdapter.FillBy(Me.HemDatabase1DataSet.partno, tsText.Text)
        'Catch ex As System.Exception
        '    System.Windows.Forms.MessageBox.Show(ex.Message)
        'End Try

        '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)

    End Sub

    'Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
    '    Try
    '        Me.PartnoTableAdapter.FillBy(Me.HemDatabase1DataSet.partno, tsText.Text)
    '    Catch ex As System.Exception
    '        System.Windows.Forms.MessageBox.Show(ex.Message)
    '    End Try

    'End Sub

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

    End Sub
End Class
Dim objfrm As New Form2
        objfrm.ShowRecords(dt)
        objfrm.ShowDialog()

' In form 2
        Public Sub ShowRecords(ByVal dt As DataTable)
        DataGridView1.DataSource = dt
    End Sub

u need to change this code to handle if u want to show only the selected items in form1 grid

Yes i want to show selected items..

What do you mean by "u need to change this code to handle" ??

Where should i place the code ??

If you are going to show only the rows whoes checkbox is checked then Either u need to get those rows in datatable and pass to the form2 or pass all the rows and then filter and show on form2.

Ok.. I got your idea.. But since i am pretty new, and this is my first project i am working on, got no idea as to how to do it and where to start from..

Can you please illustrate via code ??

Thank you..

Public Sub Showgrid(ByVal dt As DataTable)
        Dim dtref As New DataTable
        dtref = dt
        Dim dv As New DataView
        dv = dtref.DefaultView
        dv.RowFilter = "Check = True"
        DataGridView1.DataSource = dv.ToTable
       
    End Sub
here Check is the coulmn name of checkbox i have used. dv will have the filtered list of rows and assign to gridview of form 2. hope this helps u

Hello..

I am now able to fetch the first row of the gridview in the other form, for testing purposes.. However, there is some problem..
The data are being shifted by 1 column each..
For example :

I have columns titled partid, type, role, etc..
The data that belong to partid in form 1 are displayed under type in form2, Data under type in form1 are under role in form2 and likewise, all the other data are shifted !!
This is my code for form2..

Public Class Form2

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

        'Dim count, SelectedIndex As Integer

        For i As Integer = 0 To Form1.dgv1.ColumnCount - 1
            Me.dgv2.Rows(0).Cells(i).Value = Form1.dgv1.Rows(0).Cells(i).Value
        Next

    End Sub

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

    End Sub
End Class

Why cant you use dataview as shown in previous post? Let the user select the rows which he/she wants and in button click show the form 2 grid using dataview

Actually i am trying this for the first time..

By the time The previous reply was posted, i had already applied this method.. Now, re-starting it entirely would cause problems to me as i get confused !!

So, i asked in reference to this method, that i used !!

Can you please help me with this ??

I checked your way of doing it. It worked fine for me. Please check how you have defined the columns in Form2. Is it same as column 1?

Yes.. It just has an extra column for checkboxes in form 1.. Rest all is the same !!

Thats what is causing the problem i guess. create checkbox column in form2 aswell and hide it.

ohk.. thank u !! :)

Hello.. I could solve the problem.. It was what you stated.. Thank You so much for yuor guidance.. However, the main issue is unsolved..

I have a datagridview with checkbox column in form1.. When the user selects certain rows with checkbox, I wish those selected rows be visible in datagridview in form2..

Here is what i did..

First looped through rows with j, in the rows, counted columns available via i and then, tried to obtain the checked values..

For j As Integer = 0 To Form1.dgv1.RowCount - 1

            If Form1.dgv1.Rows(0).Cells(0).Value = True Then

                For i As Integer = 0 To Form1.dgv1.ColumnCount - 1
                    Me.dgv2.Rows(0).Cells(i).Value = Form1.dgv1.Rows(0).Cells(i).Value
                Next

            End If

        Next

ur passing Row(0) so you will get only first row information.

I am not even getting that !! Just -1 in the 1st row of 1st column..

Can u suggest me how should i proceede ??

Please..

I sugest you to go with dataview method which is easy to implement

Actually I am using this for the first time..

Can you please help me with the code ??

After u pointed out to my mistake about passing row(0), i also tried using variables, but m messing it up !!

Would appreciate if you could help me with the code please !!

Thank You..

'In form write this code where you calling ur form2 to be shown
        Dim objfrm As New Form2
        objfrm.Showgrid(dt)
        objfrm.ShowDialog()
' In form1 write this code
Public Sub Showgrid(ByVal dt As DataTable)
        
       Dim dtref As New DataTable
        dtref = dt
        Dim dv As New DataView
        dv = dtref.DefaultView
        dv.RowFilter = "Check = True"
        If Not dv.Count = 0 Then
            DataGridView1.DataSource = dv.ToTable
        End If

Ur changes gave me two errors..

one saying : ShowGrid is not a member of demo.Form2
error 2 : Name dt not declared..

This is my code of form2 :

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objform As New Form2()
        objform.ShowGrid(dt)
        objform.ShowDialog()

    End Sub
End Class

you must write that showgrid method and should be public.
Ignore my privious post. Just write below code in form load of form2

' Add below code in form 2

Dim dtSource As DataTable
            dtSource = Form1.DataGridView1.DataSource

            Dim dv As DataView
            dv = dtSource.DefaultView
            dv.RowFilter = "Check = True"
            If Not dv.Count = 0 Then
                DataGridView1.DataSource = dv.ToTable
            End If

finally.. I am not getting -1 in any row. column..

However, i am not getting anything.. It shows blank datagridview !!

Your help is appreciated !!

Dont know whats wrong. the above code works for me. Probably you only can debug and help urself.

I am done with my previous method i was using.. Thank you so much..

:)

Heya !! Can u plz help me check what is wrong in this piece of code ?? Please ??

Public Class Form2

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

        'Dim count, SelectedIndex As Integer

        Dim count As Integer = 0

        For j As Integer = 0 To Form1.dgv1.RowCount - 1

            If Form1.dgv1.Rows(j).Cells(0).Value = True Then


                For i As Integer = 0 To Form1.dgv1.ColumnCount - 1
                    Me.dgv2.Rows(count).Cells(i).Value = Form1.dgv1.Rows(j).Cells(i).Value
                Next

                count += 1

            End If

        Next
   End Sub
End Class

Nothing wrong.. its working for me. i have tested this code.

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.