ARTICLE
How to Iterate through the DataGrid
Tags: .NET, CInt, CType, CurrencyManager, DataGrid, DataGrid Control, DataGrid in VB.NET., DataSource, DataTable, Iterate Through DataGrid, VB.NET, Windows Controls, Windows Forms
This article describes how to iterate through the datagrid
Most of the cases we need to traverse through the DataGrid.
Here is the code snippet for that :
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cm As CurrencyManager = CType(Me.BindingContext(Me.dataGrid1.DataSource), CurrencyManager)
Dim rowCount As Integer = cm.Count
'assumes datasource is a datatable
Dim colCount As Integer = (CType(Me.dataGrid1.DataSource, DataTable)).Columns.CountDim row As Integer = 0
Do While row < rowCount
Dim col As Integer = 0
Do While col < colCount
Dim cellValue As Object = Me.dataGrid1(row, col)
Console.Write(cellValue.ToString() & " ")
col += 1
Loop
Console.WriteLine("")
row += 1
Loop
End Sub
Private cm As CurrencyManager = CType(Me.BindingContext(Me.dataGrid1.DataSource), CurrencyManager)
Private rowCount As Integer = cm.Count
'assumes datasource is a datatable
Private colCount As Integer = (CType(Me.dataGrid1.DataSource, DataTable)).Columns.Count
Dim row As Integer = 0
Do While row < rowCount
Dim col As Integer = 0
Do While col < colCount
Dim cellValue As Object = Me.dataGrid1(row, col)
Console.Write(cellValue.ToString() & " ")
col += 1
Loop
Console.WriteLine("")
row += 1
Loop