|
-
Oct 25th, 2007, 01:41 PM
#1
Thread Starter
Fanatic Member
[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.
-
Oct 25th, 2007, 02:56 PM
#2
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)
-
Oct 26th, 2007, 10:08 AM
#3
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 10:10 AM
#4
Re: [2005] Using Ctrl + C to copy rows from Datagrid
i forgot to include
Code:
imports system.text
-
Oct 26th, 2007, 10:11 AM
#5
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 10:19 AM
#6
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 10:21 AM
#7
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
-
Oct 26th, 2007, 10:25 AM
#8
Re: [2005] Using Ctrl + C to copy rows from Datagrid
i forgot to mention, you have to set
Code:
me.keypreview = true
-
Oct 26th, 2007, 10:26 AM
#9
Thread Starter
Fanatic Member
Re: [2005] Using Ctrl + C to copy rows from Datagrid
 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.
-
Oct 26th, 2007, 10:38 AM
#10
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
-
Oct 26th, 2007, 10:54 AM
#11
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 10:58 AM
#12
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?
-
Oct 26th, 2007, 11:01 AM
#13
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 11:18 AM
#14
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
-
Oct 26th, 2007, 12:47 PM
#15
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 12:53 PM
#16
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 02:37 PM
#17
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
-
Oct 26th, 2007, 02:46 PM
#18
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2007, 03:28 PM
#19
Re: [2005] Using Ctrl + C to copy rows from Datagrid
what tablestyles are you using? i'll try it
-
Oct 30th, 2007, 08:56 AM
#20
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|