Populate gridview with datareader in

Posted on 2007-10-16
Last Modified: 2013-11-26
If I populate a gridview with a DATAREADER and say set the gridview to 20 rows page page.  If I have 2,000 rows in the reader can I iterate through ALL of the rows in the datareader?

For example

    foreach (GridViewRow row in this.gridView.Rows)
            DataRowView drv = row.DataItem as DataRowView;
            EmailMessage message = new EmailMessage();
            etc etc etc

So even though I am only showing the inital 20 rows will the code above iterate through all 2,000?  Also can I do sorting on this?
Question by:mugsey
    LVL 9

    Accepted Solution

    ITs not a good idea to use the datareader to bind directly to the gridview in the first place.
    remember datareader acts as a forward only cursor. You cannot go back to the previous data in a datareader once you moved forward.
    LVL 7

    Expert Comment

    i dont agree with what srivatsavaye says...

    i'm using datareader with all my gridview and in fact this is recommend to you use since its readonly/fowardonly cursor.

    i never had any problem using datareader and i can able to do next page or previous page without any issue.

    LVL 9

    Expert Comment

    Nisar Khan,
    You cannot directly databind a datareader to a gridview and do paging and sorting. That cannot be done.

    Instead you can work around it by using arraylists by reading all the data of a datareader into an arraylist.

    Author Comment

    OK Thanks

    So How can I amend the following so its a Dataset that allows paging sorting

            Database db = DatabaseFactory.CreateDatabase();
            // Get back a DataReader
            IDataReader reader = db.ExecuteReader(CommandType.StoredProcedure, "sprocname");
            gdEmailAudit.DataSource = reader;
    LVL 7

    Expert Comment


    Author Comment

    OK thanks so how can I implement sorting with dataset, do I have to do this manually?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now