Results 1 to 15 of 15

Thread: [RESOLVED] Copy All DataGrid rows

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] Copy All DataGrid rows

    Hello.

    I am using VB.NET 2003

    I read this thread :

    http://www.vbforums.com/showthread.p...atagrid+column

    There it explains how to copy one DataGrid row. I'd like to be able to copy all the rows in the DataGrid from a Button_Click. Can anyone help?
    Last edited by GrimmReaper; Mar 10th, 2011 at 04:38 AM.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Copy All DataGrid rows

    If you want to use the technique in that post, ie a formatted string version, then its just a point of adding another loop. For example here:

    Code:
    strCell = _Grid.Item(iRow, iCol).ToString
    That line extracts from iRow which is passed in. You can just add another loop, something like this (off top of my head!):

    Code:
    For iRow As Integer = 0 To _Grid.Rows.Count - 1
    ...
    Next

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    Thanks for your advice, however, It tells me that Rows is not a member of DataGrid.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    try this:

    vb Code:
    1. Dim headers = (From ch In dgv.Columns _
    2.               Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
    3.               Select header.Value).ToArray()
    4. Dim headerText() As String = Array.ConvertAll(headers, Function(v) If(v IsNot Nothing, v.ToString, ""))
    5.  
    6. Dim items() = (From r In dgv.Rows _
    7.       Let row = DirectCast(r, DataGridViewRow) _
    8.       Where Not row.IsNewRow _
    9.       Select (From cell In row.Cells _
    10.           Let c = DirectCast(cell, DataGridViewCell) _
    11.           Select c.Value).ToArray()).ToArray()
    12.  
    13. Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
    14. For Each a In items
    15.     Dim t() As String = Array.ConvertAll(a, Function(v) If(v IsNot Nothing, v.ToString, ""))
    16.     table &= String.Join(vbTab, t) & Environment.NewLine
    17. Next
    18. table = table.TrimEnd(CChar(Environment.NewLine))
    19. Clipboard.SetText(table)

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    or is it a datagrid? why not a datagridview? they're more flexible + generally an improvement on the datagrid

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    Thanks for your advice

    DataGrid is not available in VB.NET 2003, I forgot to add the [02/03] prefix. I am so sorry - I usually do that. I am stuck with the DataGrid.

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Copy All DataGrid rows

    Maybe its Items.Count then or something, I'm sure you can look it up and work it out.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    what are you using as your datasource? it'd be easier to copy the data from the datatable.
    why are you using vb02/03? you can download the latest express version free

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    I am using this fubtion as the datasource :
    Code:
        Private Function GetProductSummary() As DataTable
    
            Dim dtSummary As DataTable
            Dim dcCol As DataColumn
            Dim txtBox As Control
    
            Try
                dtSummary = New DataTable("Order")
                dcCol = New DataColumn("ProductCode", Type.GetType("System.String"))
                dtSummary.Columns.Add(dcCol)
    
                dcCol = New DataColumn("Quantity", Type.GetType("System.Int32"))
                dtSummary.Columns.Add(dcCol)
    
                For Each txtBox In Panel1.Controls
    
                    If txtBox.GetType.Name = "TextBox" Then
    
                        Dim drProducts() As DataRow
                        drProducts = dtSummary.Select("ProductCode = '" & txtBox.Text.Trim & "'")
    
                        If drProducts.Length > 0 Then
    
                            drProducts(0).Item("ProductCode") = txtBox.Text.Trim
                            drProducts(0).Item("Quantity") += 1
    
                        Else
    
                            Dim drProduct As DataRow = dtSummary.NewRow
                            drProduct.Item("ProductCode") = txtBox.Text.Trim
                            drProduct.Item("Quantity") = 1
                            dtSummary.Rows.Add(drProduct)
    
                        End If
    
                    End If
    
                Next
    
                Return dtSummary
    
            Catch ex As Exception
    
                Throw ex
    
            End Try
    
        End Function
    Code:
       dgSummary.DataSource = GetProductSummary()
    I know about the new vesions, using them as well. but the client is stubborn and does not want to upgrade to a newer version. it sucks, but i need the money so i should make the client happy...

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    ok.declare a variable dt as datatable at form level
    when you set your dg datasource, instead of:

    vb Code:
    1. dgSummary.DataSource = GetProductSummary()
    use this:

    vb Code:
    1. dt = GetProductSummary()
    2. dgSummary.DataSource = dt

    then to copy the entire dg:

    vb Code:
    1. Private Sub copyDataGridToClipboard(ByVal dg As DataGrid)
    2.     Dim table As String = ""
    3.     Dim headers As New List(Of String)
    4.     For x As Integer = 0 To dt.Columns.Count - 1
    5.         headers.Add(dt.Columns(x).ColumnName)
    6.     Next
    7.     table &= String.Join(vbTab, headers.ToArray) & Environment.NewLine
    8.  
    9.     For Each row As DataRow In dt.Rows
    10.         Dim rowText As New List(Of String)
    11.         For x As Integer = 0 To dt.Columns.Count - 1
    12.             rowText.Add(row.Item(x).ToString)
    13.         Next
    14.         table &= String.Join(vbTab, rowText.ToArray) & Environment.NewLine
    15.     Next
    16.  
    17.     table = table.TrimEnd(New Char() {vbCr, vbLf})
    18.  
    19.     Clipboard.SetText(table)
    20.  
    21. End Sub

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    Thanks for your continued help. It seems as if :

    Code:
    List(Of String)
    Is not supported with VB.NEt 2003

    Should I try to use an ArrayList or something, and how?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    i didn't think of that. with an arraylist i think you just declare a variable as an arraylist + then use .add to add an element. it infers datatypes. i'm not sure if toarray is supported either, but if you try it you'll see...

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    OK, some progress Thanks for all the suggestions. Now, hopefully final question.

    I'm using this code now :
    Code:
        Private Sub CopyAll(ByVal dg As DataGrid)
            Dim table As String = ""
            Dim headers As New ArrayList
            For x As Integer = 0 To dt.Columns.Count - 1
                headers.Add(dt.Columns(x).ColumnName)
                table &= headers.Item(x) & Environment.NewLine
            Next
    
            For Each row As DataRow In dt.Rows
                Dim rowText As New ArrayList
                For x As Integer = 0 To dt.Columns.Count - 1
                    rowText.Add(row.Item(x).ToString)
                    table &= vbTab & rowText.Item(x) & Environment.NewLine
    
                Next
    
            Next
    
            table = table.TrimEnd(New Char() {vbCr, vbLf})
    
            Clipboard.SetDataObject(table)
    
    
        End Sub
    This is the out put I get :
    Code:
    ProductCode
    Quantity
    	1234
    	2
    	12
    	2
    	123
    	2
    	
    	194
    I'd like to have 2 separate columsn, one for ProductCode, and then one for Quantity. I know it is just some small change, but I'll really appreciate any help in getting it done.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Copy All DataGrid rows

    vb Code:
    1. Private Sub CopyAll()
    2.     Dim table As String = ""
    3.    
    4.     For x As Integer = 0 To dt.Columns.Count - 1
    5.         table &= dt.Columns(x).ColumnName & vbtab
    6.     Next
    7.     table &= Environment.NewLine
    8.  
    9.     For Each row As DataRow In dt.Rows
    10.         For x As Integer = 0 To dt.Columns.Count - 1
    11.             table &= row.Item(x).ToString & vbTab
    12.         Next
    13.         table = table.TrimEnd(New Char() {vbTab})
    14.         table &= Environment.NewLine
    15.     Next
    16.  
    17.     table = table.TrimEnd(New Char() {vbTab, vbCr, vbLf})
    18.  
    19.     Clipboard.SetDataObject(table)
    20.  
    21. End Sub

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Copy All DataGrid rows

    WOW! Perfect!! You are a genius!!

    Thank everyone!

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