Hi Scott:
I see the others have answered your question but I'd like to mention the new
stuff in ADO.NET 2.0 that addresses this issue. Also, you could create a
Subclassed grid, simple inherit from DataGird and you can come up with your
own method to load the grid from a Reader if you really need this
functionality. You're going to have to bind it to a
datatable/dataset/dataview or some other IList object (Actually my memory is
foggy now but I think you can bind to anything that implements IList) or
strongly typed collection. So you can build a datatable from the reader or
build a collection from it and bind to either of those but it'll take some
work. Since Readers work best for read only scenarios and grids are usually
used for editing in the winforms context -probably not a big need for this
whereas on the web there is b/c it's usually a read only scenario (and
although you can 'edit' in a web grid, the underlying story is a bit
different which makes the reader a nice choice)
In ASP.NET you are allowed to bind to a reader, actually
dataGrid.DataSource = myCommand.ExecuteReader(); It handles the
while(reader.Read()) for you and builds a datatable. It's a clunky
implementation though b/c the reader may not have any rows so you have to
put in an extra check to verify the grid will have something in it.
In ADO.NET 2.0 you can do something similar with the DataTable's .Load
method. You can load a DataTable from a DataReader WITHOUT reading through
it. So assuming I just called ExecuteReader on a reader named dr I could to
this:
DataTable dt = new DataTable("TableFromReader");
dt.Lead(dr); //Didn't have to do anything other than call ExecuteReader
before this somewhere
DataGrid1.DataSource = dt; //Will work in Winforms!
You can also do the reverse, get a Reader from a DataTable!!!!!
The DataTable has a GetReader method , which will give you a DataReader
from a DataTable
I have some examples of all of this here
http://www.knowdotnet.com/articles/miscadonet.html
or here
http://msmvps.com/williamryan/archive/2004/07/14/10090.aspx
--
--
W.G. Ryan, eMVP
Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
http://www.devbuzz.com |
http://www.knowdotnet.com
https://www.windowsembeddedeval.com/community/newsgroups