views:

144

answers:

2

I have a DataGridView binded to this table:

[Users]
ID Name
-- ----
11 Qwe
22 Asd

Grid is direclty binded to typed dataset table.

I have a second table like this:

[Records]
ID  UserID  Data
--  ------  ----
67  11      ....
68  11      ....

Records.UserID is connected to Users with a foreign key.

What I want to do is: when the user doubleclicks User #11 I open a new grid, binded to [Records] table but only binded to rows where UserID = 11. Doubleclick, getting ID, new grid etc. those are OK. I wouldn't had any problems if I was doing this connected with sprocs but I want it to be binded and I simply have no idea how to do this.

Can you please give me any ideas?

+1  A: 

If you are using DataSets/DataTables, then you can do something like this to filter your rows for the second grid:

string filter = String.Format("UserID = {0}", selectedUserId);
grdRecords.DataSource = ds.Tables["Records"].Select(filter);

I believe you can edit the records in the array that is returned, but you won't be able to add/remove new items to this collection. You would have to add them to the regular table, then refilter the grid.

Of course, there's all kinds of other ways to do this as well. If you are using objects that derive from IBindingListView, such as instances of the DataView class, you should have access to the Filter property. Then you can just do this:

string filter = String.Format("UserID = {0}", selectedUserId);
DataView myView;
grdRecords.DataSource = myView;
myView.Filter = filter;

And your view will stay filtered to match whatever data you place into it.

Hopefully, this post is clear enough for you to be able to work out the details... I'm a little scatterbrained today. :)

Lusid
Thanks for the idea.The problem with Filter is it retrieves all of the records first then filters. So it would be a little slow when there're a lot of records.
Armagan
Not necessarily. Code a mechanism to check the data table to see if it contains any records for a certain UserID, and if it doesn't, run a SQL statement with a WHERE clause, and append those rows to the table. Then do the filter. This will provide a caching mechanism and only pull what you need.
Lusid
A: 

In the end I had to this:

  1. Create a DB class, with a public property (MyDataSetProperty) that returns the typedDataset

  2. Create a custom Fill function in DataSet that accepts an Id parameter.

  3. Use this function to fill the table in the private typedDataset

    this.myTableAdapter.FillCustom(this.myTypedDataset.MyTable, this.Id);

  4. Bind bindingSource to this public property:

    this.bindingSource.DataSource = this.db.MyDataSetProperty;

Armagan