Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to bind a DataReader in a GridView?
Please help..
Posted
Updated 23-Feb-11 1:14am
v4
Comments
Dalek Dave 23-Feb-11 6:43am    
Edited for Spelling and Grammar.

 
Share this answer
 
Comments
Dalek Dave 23-Feb-11 6:43am    
Good Link
[no name] 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.
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,
:)
 
Share this answer
 
v2
Comments
Dalek Dave 23-Feb-11 6:43am    
Good Call.
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.
 
Share this answer
 
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);
       }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900