Results 1 to 11 of 11

Thread: [RESOLVED] selected Datagridview rows in one form to another form

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Resolved [RESOLVED] selected Datagridview rows in one form to another form

    Hello all,
    I am new to this forum and also to vb so pl. excuse me if i do any mistakes...

    I am doing a project in window application (VB 2008) where I have 2 forms (form1 & form2) in form1 I place a empty DGV and a button, and in form2 i placed another DGV which i connects to MS access DB, in form2 DGV I add a checkbox column and i need to do is if i click the button in form1 it should show the form2 DGV (till this i got it) and when i checked the rows in form2 DGV the checked rows should shown in form1 DGV. and i also want to add the sum in bottom. pl... pl... guide me.....

    Thanks in advance

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,186

    Re: selected Datagridview rows in one form to another form

    Check this & reply
    Last edited by make me rain; Nov 3rd, 2011 at 11:50 AM.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: selected Datagridview rows in one form to another form

    Quote Originally Posted by make me rain View Post
    Check this & reply
    Pl. find my application in attachment...
    This what i required actually... sorry I cant able to explain what i need, so i am attaching my application itself... Please help me to complete this task... And thanks for your quick reply and I also trying your code in my appln...
    I have no word to thank u and also this forums people they are doing excellent contribution to people like me.
    Attached Files Attached Files
    Last edited by tnncprojects; Oct 17th, 2011 at 02:23 AM.

  4. #4
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,186

    Re: selected Datagridview rows in one form to another form

    see this & reply
    Last edited by make me rain; Nov 3rd, 2011 at 11:51 AM.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: selected Datagridview rows in one form to another form

    Quote Originally Posted by make me rain View Post
    see this & reply
    You solved my task..... Great
    I have no words to express my happiness... U make me happiest person in this world and i don't know how to appreciate your work and I am very very small person in front of you... "Make me rain" you "made me to showers in my work"..... Thanks Thanks Thanks... nth times thanks.....

    and I will continue to ask my doubts to u... please don't get hesitate I know you don't....

    Once again i thank u....

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,529

    Re: selected Datagridview rows in one form to another form

    Quote Originally Posted by tnncprojects View Post
    Hello all,
    I am new to this forum and also to vb so pl. excuse me if i do any mistakes...

    I am doing a project in window application (VB 2008) where I have 2 forms (form1 & form2) in form1 I place a empty DGV and a button, and in form2 i placed another DGV which i connects to MS access DB, in form2 DGV I add a checkbox column and i need to do is if i click the button in form1 it should show the form2 DGV (till this i got it) and when i checked the rows in form2 DGV the checked rows should shown in form1 DGV. and i also want to add the sum in bottom. pl... pl... guide me.....

    Thanks in advance
    Did you try the code I suggested here http://www.vbforums.com/showpost.php...35&postcount=5

    Also in the future please do not duplicate your question. This current pose if fine but not good to post a question in another users posting.

  7. #7
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: selected Datagridview rows in one form to another form

    You will need to loop through the rows, check the value of the checkbox and if true then you can add to your sum and also add to the grid or create a datatable add a row to that then bind to the grid.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,529

    Re: selected Datagridview rows in one form to another form

    Here is a full demo where both DataGridView controls are on the same form so for your project simple adapt to referening the controls.

    Two methods are shown, first by CheckBox column checked using the following extension method to get checked rows.

    Code:
    <Runtime.CompilerServices.Extension()> _
    Public Function GetChecked( _
        ByVal sender As DataGridView, _
        ByVal ColumnName As String) As List(Of DataGridViewRow)
    
        Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
                    Where CBool(Rows.Cells(ColumnName).Value) = True).ToList
    
    End Function
    If you only want a count of checked (by column index but you can overload this function to reference by column name)
    Code:
    <Runtime.CompilerServices.Extension()> _
    Public Function CheckBoxCount( _
        ByVal sender As DataGridView, _
        ByVal ColumnIndex As Integer, _
        ByVal Checked As Boolean) As Integer
    
        Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
                Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
    
    End Function
    The following is not needed if you have columns defined in the target DataGridView.
    Code:
    DataGridView2.CloneColumns(DataGridView1)
    Since I dynamcially add columns in the target DataGridView it adds the checkbox column, I hide it in the event it is not needed or you could have an overloaded Clone function to not include a specific column or columns.
    Code:
    DataGridView2.Columns(CheckBoxColumnName).Visible = False

    The following adds the checked rows in the source DataGridView to the target DataGridView (first the rows are cleared which is optional)

    Code:
    DataGridView2.Rows.Clear()
    
    ' If this is not done it is possible to miss a row as it has not been updated
    DataGridView1.EndEdit()
    
    Dim CheckedRows = DataGridView1.GetChecked(CheckBoxColumnName).ToArray
    
    For Each row As DataGridViewRow In CheckedRows
        row.CloneRowWithValues(DataGridView2)
    Next
    Attached Files Attached Files

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: selected Datagridview rows in one form to another form

    pl. explain this line "Me.DataGridView1.DataSource = Dt" and what Dt means, FYI... I am using MS access as database to view rows in form2 DGV and in form1 DGV it is empty.

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,529

    Re: selected Datagridview rows in one form to another form

    Quote Originally Posted by tnncprojects View Post
    pl. explain this line "Me.DataGridView1.DataSource = Dt" and what Dt means, FYI... I am using MS access as database to view rows in form2 DGV and in form1 DGV it is empty.
    Me.DataGridView1.DataSource = Dt, that is not from my code.

    In regards to your project loading data from MS-Access, no problem, simply alter my form code and code modules as shown below. Note I use a BindingSource for obtaining row position of a Checked Row. I always ended up using a BindingSource as it makes life easier when working with data, at least for me.

    Form code
    Code:
    Public Class Form1
        Private CheckBoxColumnName As String = "SelectedColumn"
        WithEvents bsData As New BindingSource
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim FirstNameColumn As New DataGridViewTextBoxColumn With {.HeaderText = "First", .Name = "FirstName", .DataPropertyName = "FirstName"}
            Dim LastNameColumn As New DataGridViewTextBoxColumn With {.HeaderText = "Last", .Name = "LastName", .DataPropertyName = "LadtName"}
            Dim CheckedColumn As New DataGridViewCheckBoxColumn With {.HeaderText = "Select", .Name = CheckBoxColumnName, .DataPropertyName = "Process"}
    
            DataGridView1.Columns.AddRange(New DataGridViewColumn() {CheckedColumn, FirstNameColumn, LastNameColumn})
    
            Dim Data = MimicLoadingFromDatabase()
            bsData.DataSource = Data
            DataGridView1.DataSource = bsData
    
            Dim CloneStructure = Data.Clone
            DataGridView2.DataSource = CloneStructure
            DataGridView2.Columns(0).Visible = False
    
        End Sub
        Private Function MimicLoadingFromDatabase() As DataTable
            Dim dt As New DataTable With {.TableName = "Jobs"}
    
            With dt.Columns
                .AddRange(New DataColumn() _
                   { _
                      New DataColumn("Process", GetType(System.Boolean)), _
                      New DataColumn("FirstName", GetType(System.String)), _
                      New DataColumn("LastName", GetType(System.String)) _
                   } _
                )
            End With
    
            dt.Rows.Add(New Object() {False, "John", "Smith"})
            dt.Rows.Add(New Object() {True, "Mary", "Doe"})
            dt.Rows.Add(New Object() {True, "Piper", "Wittman"})
            dt.Rows.Add(New Object() {True, "Tom", "Wittman"})
    
            Return dt
        End Function
        Private Sub AllCheckedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllCheckedToolStripMenuItem.Click
            DataGridView1.EndEdit()
            Dim TargetTable = DirectCast(DataGridView2.DataSource, DataTable)
            TargetTable.Rows.Clear()
    
            Dim CheckedRows = DataGridView1.GetChecked(CheckBoxColumnName).ToArray
    
            For Each row As DataGridViewRow In CheckedRows
                TargetTable.ImportRow(bsData.ItemRow(row.Index))
            Next
        End Sub
        Private Sub CloseDemoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseDemoToolStripMenuItem.Click
            Close()
        End Sub
    End Class
    Module code
    Code:
    Module Module1
        <Runtime.CompilerServices.Extension()> _
        Public Function GetChecked(ByVal sender As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
            Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
                        Where CBool(Rows.Cells(ColumnName).Value) = True).ToList
        End Function
        <Runtime.CompilerServices.Extension()> _
        Public Function CheckBoxCount(ByVal sender As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer
            Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
        End Function
    
        <System.Diagnostics.DebuggerStepThrough()> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Function ItemRow(ByVal sender As BindingSource, ByVal Index As Integer) As DataRow
            Return DirectCast(sender.Item(Index), DataRowView).Row
        End Function
    End Module

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: selected Datagridview rows in one form to another form

    Thanks for your quick response... and also all people in this forum...
    I check and reply....
    Last edited by tnncprojects; Oct 17th, 2011 at 01:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width