?
Solved

Populate gridview with datareader in asp.net

Posted on 2007-10-16
6
Medium Priority
?
7,246 Views
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?
0
Comment
Question by:mugsey
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
Raju Srivatsavaye earned 2000 total points
ID: 20088152
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.
0
 
LVL 7

Expert Comment

by:nisarkhan
ID: 20088465
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.

http://www.beansoftware.com/ASP.NET-Tutorials/Data-Source-Controls.aspx

 
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 20088832
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mugsey
ID: 20092539
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;
        gdEmailAudit.DataBind();  
0
 
LVL 7

Expert Comment

by:nisarkhan
ID: 20092922
0
 

Author Comment

by:mugsey
ID: 20094110
OK thanks so how can I implement sorting with dataset, do I have to do this manually?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

569 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