Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Is it possible to bind a DataReader in a GridView?
Please help..
Posted 23-Feb-11 0:11am
Edited 23-Feb-11 1:14am
v4
Comments
Dalek Dave at 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 at 23-Feb-11 6:43am
   
Good Link
mehdi_csharp at 23-Feb-11 6:44am
   
it is for asp ,in c# do not worked!!
Prerak Patel at 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,
Smile | :)
  Permalink  
v2
Comments
Dalek Dave at 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
0 Sergey Alexandrovich Kryukov 474
1 Maciej Los 375
2 OriginalGriff 263
3 CPallini 214
4 Peter Leow 200
0 OriginalGriff 6,152
1 Sergey Alexandrovich Kryukov 5,312
2 Maciej Los 3,279
3 Peter Leow 3,184
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Feb 2011
Copyright © CodeProject, 1999-2014
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