|
-
Mar 10th, 2011, 03:58 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Mar 10th, 2011, 04:16 AM
#2
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
-
Mar 10th, 2011, 04:26 AM
#3
Thread Starter
Hyperactive Member
Re: Copy All DataGrid rows
Thanks for your advice, however, It tells me that Rows is not a member of DataGrid.
-
Mar 10th, 2011, 04:30 AM
#4
Re: Copy All DataGrid rows
try this:
vb Code:
Dim headers = (From ch In dgv.Columns _
Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
Select header.Value).ToArray()
Dim headerText() As String = Array.ConvertAll(headers, Function(v) If(v IsNot Nothing, v.ToString, ""))
Dim items() = (From r In dgv.Rows _
Let row = DirectCast(r, DataGridViewRow) _
Where Not row.IsNewRow _
Select (From cell In row.Cells _
Let c = DirectCast(cell, DataGridViewCell) _
Select c.Value).ToArray()).ToArray()
Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
For Each a In items
Dim t() As String = Array.ConvertAll(a, Function(v) If(v IsNot Nothing, v.ToString, ""))
table &= String.Join(vbTab, t) & Environment.NewLine
Next
table = table.TrimEnd(CChar(Environment.NewLine))
Clipboard.SetText(table)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 04:33 AM
#5
Re: Copy All DataGrid rows
or is it a datagrid? why not a datagridview? they're more flexible + generally an improvement on the datagrid
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 04:36 AM
#6
Thread Starter
Hyperactive Member
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.
-
Mar 10th, 2011, 04:51 AM
#7
Re: Copy All DataGrid rows
Maybe its Items.Count then or something, I'm sure you can look it up and work it out.
-
Mar 10th, 2011, 05:26 AM
#8
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 05:33 AM
#9
Thread Starter
Hyperactive Member
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...
-
Mar 10th, 2011, 05:43 AM
#10
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:
dgSummary.DataSource = GetProductSummary()
use this:
vb Code:
dt = GetProductSummary()
dgSummary.DataSource = dt
then to copy the entire dg:
vb Code:
Private Sub copyDataGridToClipboard(ByVal dg As DataGrid)
Dim table As String = ""
Dim headers As New List(Of String)
For x As Integer = 0 To dt.Columns.Count - 1
headers.Add(dt.Columns(x).ColumnName)
Next
table &= String.Join(vbTab, headers.ToArray) & Environment.NewLine
For Each row As DataRow In dt.Rows
Dim rowText As New List(Of String)
For x As Integer = 0 To dt.Columns.Count - 1
rowText.Add(row.Item(x).ToString)
Next
table &= String.Join(vbTab, rowText.ToArray) & Environment.NewLine
Next
table = table.TrimEnd(New Char() {vbCr, vbLf})
Clipboard.SetText(table)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 06:49 AM
#11
Thread Starter
Hyperactive Member
Re: Copy All DataGrid rows
Thanks for your continued help. It seems as if :
Is not supported with VB.NEt 2003
Should I try to use an ArrayList or something, and how?
-
Mar 10th, 2011, 06:54 AM
#12
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 07:19 AM
#13
Thread Starter
Hyperactive Member
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.
-
Mar 10th, 2011, 07:26 AM
#14
Re: Copy All DataGrid rows
vb Code:
Private Sub CopyAll()
Dim table As String = ""
For x As Integer = 0 To dt.Columns.Count - 1
table &= dt.Columns(x).ColumnName & vbtab
Next
table &= Environment.NewLine
For Each row As DataRow In dt.Rows
For x As Integer = 0 To dt.Columns.Count - 1
table &= row.Item(x).ToString & vbTab
Next
table = table.TrimEnd(New Char() {vbTab})
table &= Environment.NewLine
Next
table = table.TrimEnd(New Char() {vbTab, vbCr, vbLf})
Clipboard.SetDataObject(table)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2011, 07:31 AM
#15
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|