Click here to Skip to main content
12,948,567 members (63,606 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Is it possible to bind a DataReader in a GridView?
Please help..
Posted 23-Feb-11 0:11am
Updated 23-Feb-11 1:14am
v4
Comments
Dalek Dave 23-Feb-11 6:43am
   
Edited for Spelling and Grammar.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Dalek Dave 23-Feb-11 6:43am
   
Good Link
mehdi_csharp 23-Feb-11 6:44am
   
it is for asp ,in c# do not worked!!
Prerak Patel 23-Feb-11 6:47am
   
Try it, you just have to put your connection string, tablename and you don't need databind.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi mehdi_csharp,

conn.Open();
SqlCommand cmd= new SqlCommand("<your sql="" command="">", conn);
SqlDataReader dr = cmd.ExecuteReader();
Gridview1.DataSource = dr;
Gridview1.DataBind();
conn.Close();
</your>


I hope this help,
:)
  Permalink  
v2
Comments
Dalek Dave 23-Feb-11 6:43am
   
Good Call.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I pesonally wouldn't use a DataReader to bind to a GridView. I would use the reader to populate a DataTable and then bind the GridView to the DataTable Something like this

SqlConnection con = new SqlConnection("Your Connection String Here");
            con.Open();
            SqlCommand cmdQuery = new SqlCommand("Your Query Here", con);
            SqlDataReader drQuery = cmdQuery.ExecuteReader();
            DataSet dsQuery = new DataSet();
            DataTable dtQuery = new DataTable();
            dsQuery.Tables.Add(dtQuery);
            dsQuery.Load(drQuery, LoadOption.PreserveChanges, dsQuery.Tables[0]);
            DataGridView1.DataSource = dsQuery.Tables[0];


Happy Coding.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

i do it with DataGridView RadComponent..

 public Form1()
        {
            InitializeComponent();
 
            SqlCommand k = new SqlCommand()
            {
          Connection = new SqlConnection() { ConnectionString = @"Data source=.\sqldeveloper;Integrated  
                                             Security=true;Database=shop" },
 
                CommandText = "select * from customer"
 
            };
 
          k.Connection.Open();
          SqlDataReader g=  k.ExecuteReader();
 
          radGridView1.MasterGridViewTemplate.LoadFrom(g);
        }
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,419
CHill60 3,275
Maciej Los 2,778
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 23 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100