March 29, 2009

Loop through all the rows in all the pages of an ASP.NET GridView




One simple way to loop through all the rows in all the pages of a GridView is to access its DataSource. Shown here is the code of how to do so:

C#


    protected void Button1_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 Button1_Click(ByVal sender As Object, ByVal e As EventArgs)


        Dim dsaArgs As DataSourceSelectArguments = 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




Note: SqlDataSource1.Select(args) will make a call to the db every time it is accessed.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Loop through all the rows in all the pages of an ASP.NET GridView"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions