Results 1 to 18 of 18

Thread: Gridview Data

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Gridview Data

    Hi,

    I have 2 datagridview.
    DataGridView1 in Form1 and DataGridView2 in Form2.

    In DataGridView1 in Form1 i have data and one column contains Checkbox.

    What i want is when i click the checkbox in DataGridView1 in Form1 and go to Form2 and press on a button to bring only the data in DataGridView2 which i already checked in DataGridView1.

    Thanks.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Gridview Data

    Hi,

    You can do this this by checking which column has been clicked on the DataGridView and if it’s the CheckBox column in question then you can open another Form and pass the current row of the DataGridView to that form as a Parameter. You do this by creating a Constructor in the second form that can accept the incoming parameter and then its up to you to decide what you want do with it.

    As an example:-

    Here is some Form2 that accepts an incoming DataGridViewRow as an incoming parameter

    vb.net Code:
    1. Public Class Form2
    2.  
    3.   Private Property _CurrentRow As DataGridViewRow
    4.  
    5.   Public Sub New()
    6.     InitializeComponent()
    7.   End Sub
    8.  
    9.   Public Sub New(ByVal currentDGVRow As DataGridViewRow)
    10.     InitializeComponent()
    11.     'When the Form is Instantiated take the incoming parameter and assign it to a Private Property of this Form
    12.     _CurrentRow = currentDGVRow
    13.   End Sub
    14.  
    15.   Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
    16.     'Now you can do anything you want with the Current DataGridViewRow that has been passed to the Form
    17.     MsgBox(_CurrentRow.Cells(1).Value)
    18.   End Sub
    19. End Class

    And to call this Form with the Current Row of the DataGridView you can use the CellContentClick Event of the DataGridView.

    vb.net Code:
    1. Private Sub SomeDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SomeDataGridView.CellContentClick
    2.   'Check which column is being clicked first and if it’s the CheckBox Column then convert the current cell
    3.   'to a Boolean Type and See if has been checked. If so, then call Form2 and pass the CurrentRow of the
    4.   'DataGridView as a parameter to the Form
    5.   If e.ColumnIndex = 0 AndAlso CBool(SomeDataGridView.CurrentCell.EditedFormattedValue) Then
    6.     Using myDetailForm As New Form2(SomeDataGridView.CurrentRow)
    7.       myDetailForm.ShowDialog()
    8.     End Using
    9.   End If
    10. End Sub

    Hope that helps.

    Cheers,

    Ian

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

    Re: Gridview Data

    Hello,

    Note: I can not give you an example in VS2010, only have VS2012-VS2013

    The following loads data from a Text file into a DataTable, a BindingSource.DataSource is set to the DataTable, a DataGridView.DataSource is set to the BindingSource on the main form. The child form has a DataGridView with no columns and no code in the child form.

    People.txt needs to be in the bin\debug folder
    Code:
    Aliya,Prix
    Anika,Sablon
    Alan,Parkes
    Emilie,Berger
    Amari,Castellanos
    Melinda,MacNamara
    Matthew,Naik
    Annabel,Dresel
    Kelvin,Davison
    Charlie,Braithwaithe
    Marcella,Timmins
    Maximo,Hubble
    Riley,Bull
    Syed,Marcks
    Moriah,Paul
    Ari,Lindemann
    Sydney,Abraham
    Kaylee,Stam
    Jeffrey,Evans
    Cyrus,Erikson
    Maritza,Bale
    Jonatan,Plum
    Kaylah,Hall
    Mia,Friend
    Alana,Erdon
    Code:
    Public Class Form1
        WithEvents bsPeople As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt =
                (From line In IO.File.ReadAllLines("People.txt")
                        Where line.Length > 0
                        Let Items = line.Split(","c)
                        Select New With
                             {
                                 .FirstName = Items(0),
                                 .LastName = Items(1)
                             }
                ).ToDataTable
    
            dt.Columns.Add(New DataColumn("ID", GetType(System.Int32)))
            dt.Columns.Add(New DataColumn("Process", GetType(System.Boolean)))
    
            For x As Integer = 0 To dt.Rows.Count - 1
                dt.Rows(x).SetField(Of Integer)("ID", x + 1)
                dt.Rows(x).SetField(Of Boolean)("Process", False)
            Next
    
            Dim IdentifierColumn As New DataGridViewTextBoxColumn With
                {
                    .DataPropertyName = "ID",
                    .Name = "IdentifierCol",
                    .HeaderText = "ID",
                    .Visible = False
                }
            Dim ProcessColumn As New DataGridViewCheckBoxColumn With
                {
                    .Name = "ProcessCol",
                    .DataPropertyName = "Process",
                    .HeaderText = "Process"
                }
            Dim FirstNameColumn As New DataGridViewTextBoxColumn With
                {
                    .DataPropertyName = "FirstName",
                    .Name = "FirstNameCol",
                    .HeaderText = "First Name"
                }
            Dim LastNameColumn As New DataGridViewTextBoxColumn With
                {
                    .DataPropertyName = "LastName",
                    .Name = "LastNameCol",
                    .HeaderText = "Last Name"
                }
    
            DataGridView1.Columns.AddRange(
                New DataGridViewColumn() {IdentifierColumn, ProcessColumn, FirstNameColumn, LastNameColumn})
    
            bsPeople.DataSource = dt
            bsPeople.Sort = "LastName"
            DataGridView1.DataSource = bsPeople
    
            bsPeople.Position = bsPeople.Find("ID", 4)
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If DataGridView1.HasCheckedRows("ProcessCol") Then
                Dim CurrentCellData = DataGridView1.GetChecked("ProcessCol")
                Dim f As New ChildForm
                Try
                    f.DataGridView1.CloneColumns(DataGridView1, "ProcessCol")
                    If bsPeople.CurrentRowIsValid Then
                        DataGridView1.CurrentRow.CloneRowWithValues(f.DataGridView1)
                        f.ShowDialog()
                    End If
                Finally
                    f.Dispose()
                End Try
            End If
        End Sub
        Sub CloneData1(ByVal sender As DataGridView, ByVal e As DataGridViewCellEventArgs)
            Dim f As New ChildForm
            Try
                f.DataGridView1.CloneColumns(DataGridView1, "ProcessCol")
                If bsPeople.CurrentRowIsValid Then
                    DataGridView1.CurrentRow.CloneRowWithValues(f.DataGridView1)
                    f.ShowDialog()
                End If
            Finally
                f.Dispose()
            End Try
        End Sub
    End Class
    Add a code module place the following into it which will be used in the code above.
    Code:
        <System.Diagnostics.DebuggerStepThrough()> _
        <Runtime.CompilerServices.Extension()> _
        Public Function GetChecked(ByVal GridView As DataGridView, ByVal ColumnName As String) As DataGridViewRow
            Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnName).Value) = True).First
        End Function
        <System.Diagnostics.DebuggerStepThrough()> _
        <Runtime.CompilerServices.Extension()> _
        Public Function HasCheckedRows(ByVal GridView As DataGridView, ByVal ColumnName As String) As Boolean
            Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnName).Value) = True).Any
        End Function
        <Runtime.CompilerServices.Extension()> _
        Public Sub CloneColumns(ByVal Self As DataGridView, ByVal CloneFrom As DataGridView, ByVal NameToExclude As String)
    
            If Self.ColumnCount = 0 Then
                For Each c As DataGridViewColumn In CloneFrom.Columns
                    Self.Columns.Add(CType(c.Clone, DataGridViewColumn))
                Next
                If Self.Columns.Contains(NameToExclude) Then
                    Self.Columns(NameToExclude).Visible = False
                End If
            End If
    
        End Sub
        <System.Diagnostics.DebuggerStepThrough()> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Function CurrentRowIsValid(ByVal sender As BindingSource) As Boolean
            Return Not (sender.Current Is Nothing)
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView)
    
            Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
    
            For Row As Int32 = 0 To SingleRow.Cells.Count - 1
                Results.Cells(Row).Value = SingleRow.Cells(Row).Value
            Next
    
            Target.Rows.Add(Results)
    
        End Sub
    Name:  AM.jpg
Views: 324
Size:  28.9 KB

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    Thank you IanRyder for your reply.

    For the second code, where i should put it?

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    Thank you kevininstructor for your reply.

    The output is good as you have shown up, but can i get VS2010 code?

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Gridview Data

    Hi,

    As you can see I wrote my example to execute the opening of Form2 on the Clicking of the CheckBox Cell in the DataGridView which may not be exactly what you want. If you still want to try this method then, as I said, you put the second piece of code in the CellContentClick Event of the DataGridView.

    As to kevininstructor’s example, I would suggest that you just give it a try in 2010 since even though I am in the same position as kevinstructor I cannot see any reason why that would not run in 2010.

    Good luck and cheers,

    Ian

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    I wrote kevininstructor’s example, but it gives me "'TDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of<anonymouse type>)'" here:

    Code:
    Dim dt =
                (From line In IO.File.ReadAllLines("People.txt")
                        Where line.Length > 0
                        Let Items = line.Split(","c)
                        Select New With
                             {
                                 .FirstName = Items(0),
                                 .LastName = Items(1)
                             }
                ).ToDataTable

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

    Re: Gridview Data

    Seems I forget to add ToDataTable which you can located in MSDN Working with language extensions in the project Team Library,
    DataExtensions.vb

    Name:  00000 AM.png
Views: 252
Size:  29.5 KB

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

    Re: Gridview Data

    Quote Originally Posted by LeoJb View Post
    Thank you kevininstructor for your reply.

    The output is good as you have shown up, but can i get VS2010 code?
    If you please read the first sentence in my first reply.

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    So Shall i download it from the link that you have provided it?

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

    Re: Gridview Data

    No reason too, simply copy the ToDataTable from the file I mentioned, paste it into a code module (not a class or form) in the same project and use it.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    Sorry for disturbing you, but i couldn't find ToDataTable.

    What to do exactly?

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    Ok i did it. Still on thing, in the below it gives me "Type 'ChildForm' is not defined"

    Code:
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If dgv_UnbilledVouchers.HasCheckedRows("ProcessCol") Then
                Dim CurrentCellData = dgv_UnbilledVouchers.GetChecked("ProcessCol")
                Dim f As New ChildForm
                Try
                    f.DataGridView1.CloneColumns(dgv_UnbilledVouchers, "ProcessCol")
                    If bsPeople.CurrentRowIsValid Then
                        dgv_UnbilledVouchers.CurrentRow.CloneRowWithValues(f.DataGridView1)
                        f.ShowDialog()
                    End If
                Finally
                    f.Dispose()
                End Try
            End If
        End Sub
    And here:

    Code:
     Sub CloneData1(ByVal sender As DataGridView, ByVal e As DataGridViewCellEventArgs)
            Dim f As New ChildForm
            Try
                f.DataGridView1.CloneColumns(dgv_UnbilledVouchers, "ProcessCol")
                If bsPeople.CurrentRowIsValid Then
                    dgv_UnbilledVouchers.CurrentRow.CloneRowWithValues(f.DataGridView1)
                    f.ShowDialog()
                End If
            Finally
                f.Dispose()
            End Try
        End Sub

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

    Re: Gridview Data

    As stated in an earlier reply, Child Form is a form with a DataGridView. So create a form with a DataGridView and name it as I did.

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    Thank you.

    Now when i want to open the Form1 it gives me "Could not find the file 'C:\Users\..\..\..\bin\debug\People.txt'"

    Code:
    Dim dt =
                (From line In IO.File.ReadAllLines("People.txt")
                        Where line.Length > 0
                        Let Items = line.Split(","c)
                        Select New With
                             {
                                 .FirstName = Items(0),
                                 .LastName = Items(1)
                             }
                ).ToDataTable

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

    Re: Gridview Data

    You need to create the file and use the data provided in post 3.

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2014
    Posts
    51

    Re: Gridview Data

    But in this way, it will only show the data in the .txt file not from the database!

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

    Re: Gridview Data

    Quote Originally Posted by LeoJb View Post
    But in this way, it will only show the data in the .txt file not from the database!
    True as this is a simple example, it's your responsibility to work with a database.

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