How to change data displayed in one GridView depending on selection made in the second one? (ASP.NET

M

misiek

Hi all.

I have two tables in my database: TableA and TableB. Records in TableB
contain foreing key to records from TableA (it is a relation one to
many: one row from TableA could have many rows from TableB connected to
it).

In DataSet Designer I created two DataTables: TableA and TableB. They
are also connected with relation MyRelation.

In my web page I want to have two GridViews:
- GridViewA - in this one records from TableA should be displayed.
- GridViewB - in this one records from TableB should be displayed (but
only these ones, which have TableA_id foreign key equal to primary key
from row selected in GridViewA).

Futhermore I want to do sth like this:
1. I select some row in GridViewA.
2. Data in GridViewB should by dynamically changed without executing
additional query against my database.

Does anybody know how it can be done?

Thanks in advance.
 
C

chris

Off the top of my head, the way I have done it is to have GridViewA
bind to a ObjectDataSource that calls TableA and GridViewB bind to a
ObjectDataSource that calls TableB. Then TableB needs to have a
GetData method that accepts an ID to filter on. Then in GridViewB's
ODS, you pass a control parameter tied to the selectedvalue of
GridViewA and this should pass in the ID to your TableB's GetData
method. You will need to customize the GetData method to filter TableB
if you do not want it pulling from the database everytime. In fact, if
you want to keep from going to the database everytime, you will need to
store both of your datasets in data cache.

HTH,
Chris
 
M

misiek

Chris, thanks for help.

Still I have a problem: how should I pass a control parametter tied to
the selectedValue of GridViewA? And the second question: how should I
store my datasets in data cache?
Yes, I am a beginner:).

It would be great, if you provide some of your code...

Thanks,
misiek
 
M

misiek

I have one more question: I retrieved data from DB using
ObjectDataSource. Now I want to iterate through every DataRow(?) from
this ObjectDataSource. How can I do this?
 
G

Guest

Hi Misiek,

It seems that ObjectDataSource has no method or property to give us 'Data
Source' object, such as DataSet, DataTable, or DataView. However, if your
data object bound to ObjectDataSource is disconnected data object (not
datareader), you might retrieve it from data control like GridView. you can
try

DataTable (or DataView/ DataSet) data = (DataTable)GridView.DataSource;

I just got this idea, havn't try it. Let me know the result.

Thanks!

Elton
 
M

misiek

Hi Elton.

I changed my solution a bit: I store my DataView in Session and update
it whenever it is neccessary. So now user works on cached data: this
data is retrieved from DB at the beginning of his work and is saved at
the end when he presses Save button. So communication to DB is reduced
to minimum.

I also tried your solution and it looks like it works. Thanks.

Misiek.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top