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.
Printable View
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.
i found this on google. it uses these 2 functions
then to set text to the clipboardCode: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
Code:Clipboard.SetDataObject(GetGridRow(grid.CurrentRowIndex, GetMaxColumnIndex), True)
It says StringBuilder isn't Defined but gives an option of Text.StringBuilder. Should I use that?
i forgot to include
Code:imports system.text
Ok, let me do that. Thanks!
Where would I call this code in a keypress or keydown?
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
i forgot to mention, you have to setCode:me.keypreview = true
Quote:
Originally Posted by .paul.
On the form?
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
I got this error: DataGridColumnStyle of '' cannot be used because it is not associated with a Property or Column in the DataSource.
i know this works with a text only datagrid.
which line is causing the error?
It doesn't give me a line it just pops up and gives that error.
i just tested it again. it works fine.
i can only guess you have a non text column?
try F8 stepping debugging
yes, i have dates, and numbers in the 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.
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
nope, didn't work. still get that error.
what tablestyles are you using? i'll try it
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