Binding a datareader to a grid

J

John Dann

Is there a Winforms grid to which I can readily bind a datareader
(purely for read-only visualisation of the datareader data and using
the 1.1 framework)? Or is this in some way not regarded as a
legitimate use for the datareader?

JGD
 
W

William \(Bill\) Vaughn

No, you can't bind a DataReader to a DataGrid in a WinForms application.
You'll need to use Fill. Some might suggest writing code to create a
DataTable from a DataReader, but I have not seen anyone write code as
efficient as Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
E

Earl

You could use the datareader to return the data you need to populate a
datatable and bind that to a read-only datagrid.
 
J

John Dann

No, you can't bind a DataReader to a DataGrid in a WinForms application.

Thanks for the response. Really I was wondering whether there was any
alternative to a DataGrid, eg perhap a 3rd party grid control, that
would allow direct binding.

JGD
 
S

Scott M.

You can't bind a DataReader to any grid because a grid shows many records
and a DataReader is only populated with one row of data at any given time.
 
M

Miha Markic [MVP C#]

Hi John,

Why would you want to?
I mean, the data retrieved should be stored somewhere.
So, why store data into the grid (as you want)?
Isn't storing the data in universal container such as dataset a better
approach (rhetorical question ;-))?
In this way, grid (each grid) doesn't need to provide its own data container
and data is stored in a way that other controls can use it.
 
J

John Dann

Why would you want to?
I mean, the data retrieved should be stored somewhere.
So, why store data into the grid (as you want)?
Isn't storing the data in universal container such as dataset a better
approach (rhetorical question ;-))?
In this way, grid (each grid) doesn't need to provide its own data container
and data is stored in a way that other controls can use it.

Well, I can well believe that you're right and quite possibly I'm not
approaching this in the best way conceptually.

But the project calls for a subset of data from a large database to be
bound to a chart. Especially in development but also to some extent in
subsequent use I can imagine that it will be quite useful to
crosscheck unexpected chart results against the underlying data.
(Especially when, as real-time instrumental data, it won't be feasible
to rigorously validate all data that's added to the database.) The
chart might need to be refreshed every minute or more frequently and a
datareader seems to be the fastest way to make a selection from a
database that might run to a million rows or more. But creating a
dataset from the datareader and then binding that to a Datagrid in
order to visualise the data in numeric terms seems an unnecessary and
potentially slow step. Hence why I was looking for a way of binding
the datareader directly to a read-only grid.

JGD
 
W

William Ryan eMVP

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
 
S

Scott M.

Actually, I didn't ask a question, John did.


William Ryan eMVP said:
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
 
W

William \(Bill\) Vaughn

If you're considering creating a chart, I would suggest that you take a look
at SQL Server 2000 Reporting Services. It's designed specifically for your
needs.
If you want to stick with a DataGrid, consider that the Fill method uses the
DataReader to populate the DataTable. If the query is efficient, you won't
see any performance hit over writing your own code.
Yes, there might be another third-party control, but I'm not familiar with
one.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
M

Miha Markic [MVP C#]

John Dann said:
Well, I can well believe that you're right and quite possibly I'm not
approaching this in the best way conceptually.

But the project calls for a subset of data from a large database to be
bound to a chart. Especially in development but also to some extent in
subsequent use I can imagine that it will be quite useful to
crosscheck unexpected chart results against the underlying data.
(Especially when, as real-time instrumental data, it won't be feasible
to rigorously validate all data that's added to the database.) The
chart might need to be refreshed every minute or more frequently and a
datareader seems to be the fastest way to make a selection from a
database that might run to a million rows or more. But creating a
dataset from the datareader and then binding that to a Datagrid in
order to visualise the data in numeric terms seems an unnecessary and
potentially slow step. Hence why I was looking for a way of binding
the datareader directly to a read-only grid.

But still, you have to fetch and store data somewhere. That's exactly what
dataadapter does - it uses datareader to store data in dataset.
If you want to retrieve or update just a part of data - no problem,
dataadapter does it for you (based on sql select statement) assuming you
have a primary key defined (you have to have it if you want data updating).
I reallly don't see any overhead here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top