?
Solved

DataReader data bind to Datagridview

Posted on 2009-04-22
5
Medium Priority
?
1,321 Views
Last Modified: 2013-11-26
How to use Datareader to do data bind to datagridview.
pls provide vb.net 2008 sample code
0
Comment
Question by:aarontham
  • 3
5 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24211750
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
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24211765
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
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 total points
ID: 24211773
       Dim rdr As SqlDataReader = Nothing
...
        Dim t As DataTable = New DataTable()
        t.Load(rdr)
        DataGridView1.DataSource = t
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24211776
0
 
LVL 6

Expert Comment

by:MrJesse34
ID: 24263328
      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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question