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?
Printable View
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?
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:
That line extracts from iRow which is passed in. You can just add another loop, something like this (off top of my head!):Code:strCell = _Grid.Item(iRow, iCol).ToString
Code:For iRow As Integer = 0 To _Grid.Rows.Count - 1
...
Next
Thanks for your advice, however, It tells me that Rows is not a member of DataGrid.
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)
or is it a datagrid? why not a datagridview? they're more flexible + generally an improvement on the datagrid
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. :(
Maybe its Items.Count then or something, I'm sure you can look it up and work it out.
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
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
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...Code:dgSummary.DataSource = GetProductSummary()
ok.declare a variable dt as datatable at form level
when you set your dg datasource, instead of:
use this:vb Code:
dgSummary.DataSource = GetProductSummary()
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
Thanks for your continued help. It seems as if :
Is not supported with VB.NEt 2003Code:List(Of String)
Should I try to use an ArrayList or something, and how?
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...
OK, some progress :) Thanks for all the suggestions. Now, hopefully final question.
I'm using this code now :
This is the out put I get :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
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.Code:ProductCode
Quantity
1234
2
12
2
123
2
194
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
WOW! Perfect!! You are a genius!!
Thank everyone!