One simple way to loop through all the rows in all the pages of a GridView is to access its DataSource. In this example, we will loop through the SQLDataSource to retrieve all the rows in a GridView and access its cell value. You can modify the logic depending on the type of controls you have added to the GridView
C#
protected void btnLoop_Click(object sender, EventArgs e)
{
DataSourceSelectArguments dsaArgs = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(dsaArgs);
DataTable dt = view.ToTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string s = dt.Rows[i][j].ToString();
}
}
}
VB.NET
Protected Sub btnLoop_Click(ByVal sender As Object, ByVal e As EventArgs)Note: If you loop through the GridView instead of the DataSource, you will loop through the rows only for 'that current page'
Dim dsaArgs As New DataSourceSelectArguments()
Dim view As DataView = CType(SqlDataSource1.Select(dsaArgs), DataView)
Dim dt As DataTable = view.ToTable()
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
Dim s As String = dt.Rows(i)(j).ToString()
Next j
Next i
End Sub
Tweet
1 comment:
I have three GridViews with paging enabled. I need to automatically loop through all pages of each table at a set time interval without user interaction. Can you expand on the post above so I might implement it in my solution.
Post a Comment