Using DataReader to fill DataGrid.

G

Geir Holme

Hi all.
As I read about .NET i see that we should use a datareader to read data.
Well, that's OK, but is this det best way of filling a Datagrid just for
reading. I don't want to use a listboks because I want to add some collors
to the cells depending on the values and so on. So, is there an easy way to
fill a datagrid from a datareader without looping arroun each row and each
cell. If not, is this looping more effektive than connecting a dataset to
the datagrid an .fill the adapter?

Or is there another fast and easy way of filling the datagrid.

And some simple ones.

Does the datagrid support multirow. (One record displayed over 2 rows in the
grid).
Does the combo in .NET support multicolumn. (Typically hide the ID column).

Thanx all


-geir
 
W

William Ryan eMVP

Geir:

You can only use a DataReader to bind to a ASP.NET Grid...it won't work for
a Windows.Forms grid. Since the ASP.NET grid doesn't support editing , you
have to kind of work it in behind the scenes. Anyway, the main reason you'd
go with a datatable is for paging, sorting and filtering (although from what
I've seen, that's all going to be built into ASP.NET 2.0)

If you are using a Web Grid, just the teh datasource = cmd.ExecuteReader();

If it's a desktop grid, you are going to have to loop. The overhead
difference can be big between using a reader and a DataAdapter but if you
keep your queries small, everything should be 'ok' performance wise. If you
are looping though, you are going to create a datatable and bind to it
anyway so any performance increase will be more than offset by the code
complexity.

AFAIK, there's not native support for the two row thing, but like anything
else, you can provide that functionality yourself. If you check out
www.knowdotnet.com Les' new grid class does have a multiline textbox column
which may be what you need.

If you are going to hide a field in a combobox, then just use the
DisplayMember (the datafield you want to appear in the combobox) if you are
using Windows Forms or DataTextMember for the Web, and ValueMember (the
actual value field you want, in this instance, the 'hidden field') or
DataValueField if you are using the web. This is a lot cleaner and you can
used SelectedItem to get the text values, and SelectedValue to get the ID or
hidden field.



HTH,

Bill
 
K

Kevin Yu [MSFT]

Thanks Bill for the quick response!

Hi Geir,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to fill a
DataGrid using ADO.NET. If there is any misunderstanding, please feel free
to let me know.

Just as Bill mentioned, DataReader can only be bond as the data source of
ASP.NET web grids. It doesn't work on a windows form grid.

If your datagrid is just for reading, I think the best way is to fill a
DataSet and set the DataSet as the data source of DataGrid. Filling a
DataSet object creates a disconnected environment of data accessing. When a
DataSet is fiiled, the data is dumped to local memory. We needn't maintain
a connection to the source database, so it occupies fewer resource. Since
the data for DataGrid are in local memeory, it will be more effective to
manipulate on the DataSet than make round trips to the server. Here is an
example of using DataSet:

DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
this.dataGrid.DataSource = ds;

I agree with Bill that if you need to hide an ID field using combobox, you
can just set the DisplayMember to the text field and set ID field as
ValueMamber.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Geir,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Geir

Hi.
Sorry for the late response and thank you for monitoring.

Well, as far as I can read the messages it looks like I am going to use
the DS no mather what. We do list lots of searches into grids (e.g.
search for customer before placing a order on it, Old ordres,
invoices....) We need the grid here to display this nice and pretty.
Even have som collors changing based on some values. We don't need to
update anything, but it looks like the DS is the way to go.

Thanks all.

I'll be back.

Best regards
Geir
MultiCase Norway
 
K

Kevin Yu [MSFT]

Hi Geir,

It's hard to say which one is better, and I think it all depends on the
situation you have. DataSet dumps data to local memory in a disconnected
situation. When you do search or filter, DataSet is much more faster. While
DataReader requires a constantly opened connection to the server. It is
faster in forward-only browse to data and requires less resource on client.
However, I think in your situation, it's better to use DataSet.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Geir,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Geir

Hi.

Well, as far as I can read the messages it looks like I am going to use
the DS no mather what. We do list lots of searches into grids (e.g.
search for customer before placing a order on it, Old ordres,
invoices....) We need the grid here to display this nice and pretty.
Even have som collors changing based on some values. We don't need to
update anything, but it looks like the DS is the way to go.

Thanks all.

Best regards
Geir
MultiCase Norway
 
K

Kevin Yu [MSFT]

Hi Geir,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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