• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

looping through datagrid on windows form/

Hi experts!

I have created datagrid on windows form and i displayed records from database.

The datagrid has about 50 records in it,
How can i loop throgh datagrid and print all the records?

Thanks
0
LorneBackler
Asked:
LorneBackler
  • 2
1 Solution
 
checooCommented:
something like the following should work

    Public Function GetSelectedRows(ByVal dg As DataGrid)
        Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)
        Dim dv As DataView = CType(cm.List, DataView)
        Dim i As Integer
        For i = 0 To dv.Count - 1
            'write code to print your coulmns
        Next
    End Function
0
 
checooCommented:
instead of

        Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)
        Dim dv As DataView = CType(cm.List, DataView)

if you have a DataSet then you can directly create the dataview from the dataset
0
 
amyhxuCommented:
If your datagrid's datasource is a datatable:

Dim s As String
Dim i As Integer
Dim j As Integer
For i = 0 To myDataSet.Tables("myTable").Rows.Count - 1
    For j = 0 To 5  ' Suppose you have 6 columns in datagrid
        s &= DataGrid1(i, j)
        If j < 5 Then  ' Last column doesn't need " | " to the right of it
             s &= " | "
        End If
    Next
    If i < myDataSet.Tables("myTable").Rows.Count - 1 Then  ' Last row doesn't need line carriage
        s &= vbCrlf
    End If
Next

Console.WriteLine(s)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now