Click here to Skip to main content
12,074,494 members (62,903 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

90.9K views
1 download
28 bookmarked
Posted

dataGridview Using DataReader

, 24 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to Fill dataGridview Using DataReader in Win App

Introduction

One of the most questions asked to me, how to fill dataGridview Using DataReader, if you note the behavior of DataReader you will note that when you read using DataReader you read row by row so you you make looping to read all records if you want to display all record or any element from record , in other hand dataGridview need to take all data of your records in one package, so the solution is to make centralize store by make class that contains properties and set value for each properties when you read the value of each record and store each object from class that you create in ArrayList so the ArrayList will represent your store.

Using the Code

Step1: Create class that contains properties

public class MyDetails
    {
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
           } 
        }

Step 2: create ArrayList to represent your store

ArrayList sequence = new ArrayList();

Step 3: When you retrieve do the following

while (reader.Read())
               {
                   MyDetails m = new MyDetails();
                   m.Id = (int)reader[0];
                   m.Name = reader[1].ToString();
                   m.Age = (int)reader[2];
                   sequence.Add(m);
               }
               dataGridView1.DataSource = sequence;

The Final Code

SqlConnection sqlCon = null;
           try
           {
               sqlCon = new SqlConnection();
               sqlCon.ConnectionString = "Your Connection String";
               SqlCommand cmd = new SqlCommand();
               cmd.Connection = sqlCon;
               cmd.CommandText = "SELECT * FROM StudentInfo";
               sqlCon.Open();
               SqlDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   MyDetails m = new MyDetails();
                   m.Id = (int)reader[0];
                   m.Name = reader[1].ToString();
                   m.Age = (int)reader[2];
                   sequence.Add(m);
               }
               dataGridView1.DataSource = sequence;
           }
           finally
           {
               sqlCon.Close();
           }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahmed R El Bohoty
Web Developer ITeShare
Egypt Egypt
I am interested in Software architecture, Requirements Engineering and coding. I am MCP ,SCJP,MCTs,MCPD, Brainbench.

http://ahmedbohoty.blogspot.com/

You may also be interested in...

Comments and Discussions

 
QuestionMessage Automatically Removed Pin
18-Jun-15 17:35
memberpaul000918-Jun-15 17:35 
Questionobject not set to an instance issue plz correct me Pin
jitendra jayswal3-Mar-14 8:34
memberjitendra jayswal3-Mar-14 8:34 
GeneralMy vote of 1 Pin
Affan Saied13-Oct-13 22:04
memberAffan Saied13-Oct-13 22:04 
QuestionThanks. Pin
cmcginty19-Jun-13 17:34
membercmcginty19-Jun-13 17:34 
Generalthanks Pin
ali.yazdi.136216-Oct-12 10:14
memberali.yazdi.136216-Oct-12 10:14 
GeneralMy vote of 1 Pin
m137024-Sep-12 0:09
memberm137024-Sep-12 0:09 
GeneralMy vote of 5 Pin
Member 796895313-Jun-11 9:53
memberMember 796895313-Jun-11 9:53 
GeneralExcellent Pin
Honeyboy_205-Sep-10 16:23
memberHoneyboy_205-Sep-10 16:23 
GeneralMy vote of 5 Pin
Honeyboy_205-Sep-10 16:23
memberHoneyboy_205-Sep-10 16:23 
GeneralGood But Pin
Vigneshb61-Jun-10 23:53
memberVigneshb61-Jun-10 23:53 
Generalgood Pin
Donsw22-Jan-09 7:55
memberDonsw22-Jan-09 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160208.1 | Last Updated 24 Sep 2008
Article Copyright 2008 by Ahmed R El Bohoty
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid