Link to home
Start Free TrialLog in
Avatar of LorneBackler
LorneBackler

asked on

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
Avatar of checoo
checoo

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
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
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)
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial