DataReader data bind to Datagridview

How to use Datareader to do data bind to datagridview.
pls provide vb.net 2008 sample code
aaronthamAsked:
Who is Participating?
 
wht1986Connect With a Mentor Commented:
       Dim rdr As SqlDataReader = Nothing
...
        Dim t As DataTable = New DataTable()
        t.Load(rdr)
        DataGridView1.DataSource = t
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
Should be  as :
sqlCommand cmd;
SqlDataReader dr;
cmd.tex = select * from table where cutomerid= "textbox1.value"

dr = cmd.ExecuteReader();

GridView1.Bind = dr;
GridView.fill();
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
But I don't think you can bind a Windows Forms DataGridView to a DataReader. What I would do instead is create a DataTable from the DataReader and use that as the DataGridView's DataSource
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
0
 
MrJesse34Commented:
      Dim rdr As SqlDataReader = Nothing
...
        Dim t As DataTable = New DataTable()
        t.Load(rdr)
        DataGridView1.DataSource = t

That is correct, except you need to have the t.Load wrapped in a using statement to ensure proper garbage collection.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.