binding datareader to datagrid in C# windows form

J

Jason Huang

Hi,

Is it possible to bind DataReader to a DataGrid in C# windows form? And
how?
And can we update data in a DataSet by using the DataReader?
Thanks for help.


Jason
 
O

Octavio Hernandez

Is it possible to bind DataReader to a DataGrid in C# windows form?

No you can't directly. Read the data into a DataSet, and bind the grid to
it.
And can we update data in a DataSet by using the DataReader?

No, DataReaders represent a READ ONLY, FORWARD ONLY cursor. Use a
DataAdapter in order to update the data.

Regards - Octavio
 
S

sinhaar

Hi Jason,
I don't think it'll be possible.
1) DataReader does not implement any specific interface so that it can
be bindable
2) DataReader is for reading data.So if some how u r able to bind
that,it won't allow u to edit that.So it's of no use.
3) DataReader is forward only,that means u can get a record at a time
but can't traverse back to the prev record.
I think the last (most important one!!)reason would be ---
ADO.NET follows Disconnected Model.But DataReader operates in
connected mode.So there is no scope to retrieve data from data source
and then modify it and save it,in case of DataReader.

If u have such a requirement then use DataSet/Disconeccted model
instead of datareader. :)

From
Arindam
 
O

Octavio Hernandez

The DataReader allows us to read rows from a result set and store them in a
DataTable (DataSet).
For instance, the DataAdapter internally uses a DataReader in order to
execute the SELECT statement.

Regards - Octavio
 
U

Usenet User

Then why do we need a DataReader? Sounds that DataSet can do anything.

Jason,

A DataSet cannot do anything on its own. It merely represent a
disconnected chunk of data with any data updates and, optionally,
validation errors, nothing else. You can use it in a variety of ways,
including data binding.

In general, you'd use a DataReader or DataAdapter to populate a
DataSet, make changes to the data in the DataSet either directly or
thru data binding and then use a DataAdapter to submit your changes to
the database.

The above briefly describes the disconnected nature of ADO.NET: you
can acquire any data in a form of a DataSet, pass it around as much as
you like, within a single application or between many apps, make
changes to it (inserts/updates/deletes), and then hand it back to a
DataAdapter to submit the changes to the underlyng data storage.
 

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