Results 1 to 20 of 20

Thread: [2005] Using Ctrl + C to copy rows from Datagrid

  1. #1

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    [2005] Using Ctrl + C to copy rows from Datagrid

    Can anybody help me how to use Ctrl + C to copy rows from a datagrid? I don't know where to start or how to do it.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    i found this on google. it uses these 2 functions

    Code:
    Private Function GetMaxColumnIndex() As Integer
            'Cruddy hack to count the columns currently displayed in the grid. 
            'Starting at cell (0,0), we iterate through columns until an ArgumentOutOfRangeException occurs.
            'If there are no columns, no grid, or no data source, the iterator won't have been incremented
            'before an exception is encountered, and -1 will be returned.
    
            Dim i As Integer = 0
            Dim obj As Object
    
            Try
    
                Do
                    obj = Grid.Item(0, i)
                    i += 1
                Loop
    
            Catch ix As ArgumentOutOfRangeException
    
                If i > 0 Then
                    Return i
                Else
                    Return -1
                End If
    
            Catch ex As Exception
                Return -1
            End Try
    
        End Function
    
        Private Function GetGridRow(ByVal iRow As Integer, ByVal iMaxColIndex As Integer) As String
            'get a string representing the cells in datagrid row iRow, separated by tabs. 
            'Puts a vbcrlf at the end of the line.
    
            Dim sb As StringBuilder
            Dim iCol As Integer
            Dim c As DataGridCell
            Dim strCell As String
    
            sb = New StringBuilder
    
            Try
                'iterate through columns and append cell value at each column to result string
                For iCol = 0 To iMaxColIndex
                    Try
                        strCell = _Grid.Item(iRow, iCol).ToString
                    Catch
                        strCell = ""
                    End Try
                    sb.Append(strCell)
                    sb.Append(vbTab)
                Next
    
                sb.Append(vbCrLf)
    
            Catch
                'Maybe iRow isn't a valid reference or something - no reason to interrupt life, 
                'worst case is we return an empty string.
            End Try
    
            Return sb.ToString
    
        End Function
    then to set text to the clipboard

    Code:
    Clipboard.SetDataObject(GetGridRow(grid.CurrentRowIndex, GetMaxColumnIndex), True)

  3. #3

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    It says StringBuilder isn't Defined but gives an option of Text.StringBuilder. Should I use that?
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    i forgot to include

    Code:
    imports system.text

  5. #5

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    Ok, let me do that. Thanks!
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  6. #6

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    Where would I call this code in a keypress or keydown?
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    use the 2 functions above with this code

    Code:
    Public Class form1
    
        Dim CTRL_pressed As Boolean = False
    
        Private Sub form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Control Then
                CTRL_pressed = True
            End If
        End Sub
    
        Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If LCase(e.KeyChar) = "c" And CTRL_pressed Then
                Clipboard.SetDataObject(GetGridRow(grid.CurrentRowIndex, GetMaxColumnIndex), True)
            End If
        End Sub
    
        Private Sub form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            If e.KeyCode = Keys.Control Then
                CTRL_pressed = False
            End If
        End Sub
    
    End Class

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    i forgot to mention, you have to set
    Code:
    me.keypreview = true

  9. #9

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    Quote Originally Posted by .paul.
    i forgot to mention, you have to set
    Code:
    me.keypreview = true

    On the form?
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    you can set it in the form's properties window.

    this might help too

    Code:
    Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If LCase(e.KeyChar) = "c" And CTRL_pressed Then
                Clipboard.SetDataObject(GetGridRow(grid.CurrentRowIndex, GetMaxColumnIndex), True)
                e.Handled = True
            End If
        End Sub

  11. #11

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    I got this error: DataGridColumnStyle of '' cannot be used because it is not associated with a Property or Column in the DataSource.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    i know this works with a text only datagrid.

    which line is causing the error?

  13. #13

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    It doesn't give me a line it just pops up and gives that error.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    i just tested it again. it works fine.

    i can only guess you have a non text column?

    try F8 stepping debugging

  15. #15

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    yes, i have dates, and numbers in the datagrid.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  16. #16

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    Ok, I guess it doesn't like the tablestyle that is on the grid because once i commented out that code, it now works.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    did that answer your question?

    you could probably turn tablestyles on + off

    Code:
    Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If LCase(e.KeyChar) = "c" And CTRL_pressed Then
               ''turn off  tablestyle
               Clipboard.SetDataObject(GetGridRow(grid.CurrentRowIndex, GetMaxColumnIndex), True)
                e.Handled = True
                ''turn on tablestyle
            End If
    End Sub

  18. #18

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    nope, didn't work. still get that error.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    what tablestyles are you using? i'll try it

  20. #20

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Using Ctrl + C to copy rows from Datagrid

    It seems like when I have the tablestyle on, it doesn't even hit the code to copy but when it's off, it does.
    Code:
     Public Sub DisplayDgridInj()
    
            aGridTableStyle.MappingName = "ProdInj"
            Dim aCol1 As New DataGridTextBoxColumn
            Dim aCol2 As New DataGridTextBoxColumn
            Dim aCol3 As New DataGridTextBoxColumn
            Dim aCol5 As New DataGridTextBoxColumn
            Dim aCol6 As New DataGridTextBoxColumn
            Dim aCol7 As New DataGridTextBoxColumn
            Dim aCol8 As New DataGridTextBoxColumn
            Dim aCol9 As New DataGridTextBoxColumn
            Dim aCol10 As New DataGridTextBoxColumn
    
            With aCol1
                .MappingName = "API"
                .HeaderText = "API"
                .Alignment = HorizontalAlignment.Left
                .Width = 100
            End With
    
            With aCol2
                .MappingName = "StartDate"
                .HeaderText = "Start Date"
                .Alignment = HorizontalAlignment.Left
                .Width = 100
            End With
    
            With aCol3
                .MappingName = "EndDate"
                .HeaderText = "End Date"
                .Alignment = HorizontalAlignment.Left
                .Width = 100
            End With
    
            With aCol5
                .MappingName = "InitialRate"
                .HeaderText = "InitialRate"
                .Alignment = HorizontalAlignment.Right
                .Format = "#0.00"
                .Width = 100
            End With
    
            With aCol6
                .MappingName = "FinalRate"
                .HeaderText = "FinalRate"
                .Alignment = HorizontalAlignment.Right
                .Format = "#0.00"
                .Width = 100
            End With
    
            With aCol7
                .MappingName = "DeclineRate"
                .HeaderText = "DeclineRate"
                .Alignment = HorizontalAlignment.Right
                .Format = "#0.00"
                .Width = 100
            End With
    
            With aCol10
                .MappingName = "ExtendDate"
                .HeaderText = "ExtendDate"
                .Alignment = HorizontalAlignment.Center
                .Width = 100
            End With
    
            With aGridTableStyle.GridColumnStyles
                .Add(aCol1)
                .Add(aCol2)
                .Add(aCol3)
                .Add(aCol5)
                .Add(aCol6)
                .Add(aCol7)
                .Add(aCol8)
                .Add(aCol9)
                .Add(aCol10)
            End With
            dgFutWellData.TableStyles.Add(aGridTableStyle)
            dgFutWellData.CaptionText = "Future Well Data - Injection"
            dgFutWellData.DataSource = RgDataSet2.Tables("ProdInj")
        End Sub
    Code:
    If FutWellType = "Production" Then
                Me.DisplayDgridProd()
            ElseIf FutWellType = "Injection" Then
    
                'dgFutWellData.CaptionText = "Future Well Data - Injection"
                'dgFutWellData.DataSource = RgDataSet2.Tables("ProdInj")
                Me.DisplayDgridInj()
            ElseIf FutWellType = "Schedule" Then
                Me.DisplayDgridSched()
            End If
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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