Sorting DataView

H

Hemang Shah

Hello

I've been posting this question for a while without any reply.

I'm gonna try again.

Is it possible to sort the related table in a Dataview ?

I know how to sort the main table in the dataview.

Is it possible to sort the child table related to the primary table ?

Thanks
 
W

W.G. Ryan MVP

Hemang Shah said:
Hello

I've been posting this question for a while without any reply.

I'm gonna try again.

Is it possible to sort the related table in a Dataview ?
There is no sort method on a DataTable. If you need the data sorted, you
can use the DataView or use a DataTable.Select or similar method and put
them into a new datatable in the order you need them to appear. However, I
won't go as far as to say you'd never need to do this, but it's hard to
imagine a situation where the need for sorted data couldn't be satisified
using the dataview - and since you can always reference the table the view
is built on - you can probably get anything you need using this combination.
But if not, you can use the dataview, walk through the rows and then add the
values one by one to a new datatable, in essence creating a new datatable
that's sorted. Or you can use DataTable.Select and accomplish the same
thing. Remember though that this is inefficient in terms of processing as
well as storage - on a large datatable you'll be creating at a minimum twice
the memory consumption that you otherwise would.

I know how to sort the main table in the dataview.

Is it possible to sort the child table related to the primary table ?

You can sort any table by creating a view on it. But specifically if you're
asking if you can sort only the related rows, well, technically you can but
you'd have to do it essentially the same way as before. What I'd do though
is create a dataview restricting the view with the rowfilter to only the
parent rows and then sort the view. Normally, if you want only the child
rows you would want to use GetChildRows method b/c it's a lot more efficient
(it doesn't create a new view which resetting the rowfilter does) but if I
understand your question correctly, this should work for you.

On both questions though, if I misunderstood your question(s), please let em
know and I'll do what I can to address it for you.

Bill
 
C

Cor Ligthert [MVP]

Bill,
Remember though that this is inefficient in terms of processing as well
as storage - on a large datatable you'll be creating at a minimum twice
the memory consumption that you otherwise would.

Why it are only the object references.

Be aware that in 2005 there will be a build in sort for the datatable using
the dataview.
(Internally probably the same as the sample I often show).

Cor
 
W

W.G. Ryan MVP

Cor:

I'm not sure I understand your point, but if you're saying that it won't be
heavier in terms of memory consumption b/c it will be comprised of only
references, that's not correct. If you create a new datatable and iterate
through the original one, creating a new row each time and using the
original values, you won't have only references. This would be no different
than the overhead associated with hard coding the values on a new row. I
could construct an example to prove it, but I suspect you may have just
misunderstood what I was saying.
 
S

Sahil Malik [MVP]

Cor,

Here is the full paragraph ---

But if not, you can use the dataview, walk through the rows and then add the
values one by one to a new datatable, in essence creating a new datatable
that's sorted. Or you can use DataTable.Select and accomplish the same
thing. Remember though that this is inefficient in terms of processing as
well as storage - on a large datatable you'll be creating at a minimum twice
the memory consumption that you otherwise would.

...................

And also consider that it isn't just memory that you are talking about. It's
not like a DataView has a clustered index to help itself.

And one datarow cannot exist in more than one datatable at a time, so you
would have to create a brand new datarow. So in that sense, you would have
more than twice the memory usage - one of the datarow, and a bit more for
the data, so in that sense what bill said "minimum twice the memory
consumption" is right ..

What am I missing?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
C

Cor Ligthert [MVP]

Bill,

No I made a mistake you are right, see at answer by Sahil,

And now I am curious how it is done in Net. 2.0

Cor
 
C

Cor Ligthert [MVP]

Bill,

Although that I see now that I can make it in a way to overcome that memory
usage.
However that is syntatic because the GC will not work than all the time.

(Just removing the added row)

Cor
 
H

Hemang Shah

Thanks for the reply Bill

Let me try to explain again as to what I want.

I have one dataset with lets say 5 tables, and they are related to each
other with relations.

I have a form, with 4 tabs.

I created a dataview on the primary table so that I can sort by first name /
last name.

The related data from the child tables are shown in the respective tabs.

I'm using bound forms with data binding.

So the fields on the form (not the tabs) is bound to the dataview.
The fields on the tabs (which are of the child table) are bound via a
dataview using its relationname.

So that whenever another records is selected on the main form, the
respective tabs are populated with the related child data.

Now, I know how to change the sort order of the main form, because its a
dataview.

What I want is to sort the data in the tabs (which is from a child table)
not by its foriegn field, but by another field like date or wotever.

Yes, I can create another dataview for this table and sort by it, but then
there would be different currency managers and the two tables won't be in
sync.

I don't know how much was I able to explain, but I can make a small example
and send it, if this was not clear enough.

Thanks for helping!
 
B

Bart Mermuys

Hi,

Hemang Shah said:
Thanks for the reply Bill

Let me try to explain again as to what I want.

I have one dataset with lets say 5 tables, and they are related to each
other with relations.

I have a form, with 4 tabs.

I created a dataview on the primary table so that I can sort by first name
/ last name.

The related data from the child tables are shown in the respective tabs.

I'm using bound forms with data binding.

So the fields on the form (not the tabs) is bound to the dataview.
The fields on the tabs (which are of the child table) are bound via a
dataview using its relationname.

So that whenever another records is selected on the main form, the
respective tabs are populated with the related child data.

Now, I know how to change the sort order of the main form, because its a
dataview.

What I want is to sort the data in the tabs (which is from a child table)
not by its foriegn field, but by another field like date or wotever.

Yes, I can create another dataview for this table and sort by it, but then
there would be different currency managers and the two tables won't be in
sync.

I don't know how much was I able to explain, but I can make a small
example and send it, if this was not clear enough.

Thanks for helping!

Both your parent and child controls are actually bound to a DataView, the
difference is that the parent binding is more explicit.

The framework will create a new DataView(*) for the child controls, when :
- the child controls are bound,
- the parent' row position changes.

1.
Since a new DataView is created when the parent row position changes, it
might be usefull to use a default sorting order for the created child views,
which can be done using the DefaultViewManager of the DataSet, eg:

DataSet ds = <your tables & relations>
ds.DefaultViewManager.DataViewSettings[someChildTable].Sort = ".....";

Changing this setting this will only have an effect before binding or after
the parent row position changes.


2.
You can also access the DataView from one of the child bindings, the
DataSource and DataMember used to get the CurrencyManager should be the same
as you used to bind the child controls :

DataView childDV = (DataView)((CurrencyManager)BindingContext[masterView,
"RelationToChildTable"]).List;
childDV.Sort = ".....";

Changing this settting will have an effect after binding but only until the
parent row position changes.


HTH,
Greetings

* In reality a RelatedDataView is created but it inherits from DataView.
 
N

news.microsoft.com

So is it possible ? or even recommended ?


Bart Mermuys said:
Hi,

Hemang Shah said:
Thanks for the reply Bill

Let me try to explain again as to what I want.

I have one dataset with lets say 5 tables, and they are related to each
other with relations.

I have a form, with 4 tabs.

I created a dataview on the primary table so that I can sort by first
name / last name.

The related data from the child tables are shown in the respective tabs.

I'm using bound forms with data binding.

So the fields on the form (not the tabs) is bound to the dataview.
The fields on the tabs (which are of the child table) are bound via a
dataview using its relationname.

So that whenever another records is selected on the main form, the
respective tabs are populated with the related child data.

Now, I know how to change the sort order of the main form, because its a
dataview.

What I want is to sort the data in the tabs (which is from a child table)
not by its foriegn field, but by another field like date or wotever.

Yes, I can create another dataview for this table and sort by it, but
then there would be different currency managers and the two tables won't
be in sync.

I don't know how much was I able to explain, but I can make a small
example and send it, if this was not clear enough.

Thanks for helping!

Both your parent and child controls are actually bound to a DataView, the
difference is that the parent binding is more explicit.

The framework will create a new DataView(*) for the child controls, when :
- the child controls are bound,
- the parent' row position changes.

1.
Since a new DataView is created when the parent row position changes, it
might be usefull to use a default sorting order for the created child
views, which can be done using the DefaultViewManager of the DataSet, eg:

DataSet ds = <your tables & relations>
ds.DefaultViewManager.DataViewSettings[someChildTable].Sort = ".....";

Changing this setting this will only have an effect before binding or
after the parent row position changes.


2.
You can also access the DataView from one of the child bindings, the
DataSource and DataMember used to get the CurrencyManager should be the
same as you used to bind the child controls :

DataView childDV = (DataView)((CurrencyManager)BindingContext[masterView,
"RelationToChildTable"]).List;
childDV.Sort = ".....";

Changing this settting will have an effect after binding but only until
the parent row position changes.


HTH,
Greetings

* In reality a RelatedDataView is created but it inherits from DataView.
 

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