dataview sorting problem -

G

Guest

Hi
I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table
I have used two methods in order to determine the sort order of the DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, in Window2K server"). First of all, here are the two methods I have used in order to apply the sorting property to the DataView

1. Simply defining the sort order and colum
DataView dvReturn

string szSort = "DataRowID" + " ASC"
vReturn.Sort = szSort

2. Defining a primary key and then setting the default sort order
propert
/// when creating the datatable firs
DataTable dt
dt.PrimaryKey = new DataColumn[] {dt.Columns["DataRowID"]}
DataView dvFinal = new DataView(dt)

// then set the propert
dvFinal.ApplyDefaultSort = true

When I attempt to access the data from the DataView, the information is still listed as if it hasn't been sorted. Here is a sample of the code I have implemented in order to retrieve the sorted information (dvReturn would be the DataView I applied the sorting properties to

int iRowID = System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString())
int iEmpStatusID = System.Convert.ToInt32(dvReturn[j]["EmpStatusTypeID"].ToString())

Any help would be greatly appreciated, as it is not obvious to me what I am doing wrong. Thank you in advance

jwede
 
W

William Ryan eMVP

Jwedel:

If you call ApplyDefaultSort after the sort, it will undo the sort you
specified and use the original one. What exactly is that last reference
doing? It looks like you are just referencing one row, so whatever J is
will determine the sort order. Nonetheless, if you just use Sort like you
did in the first one, regardless of primary key, you'll sort on that field.
jwedel_stolo said:
Hi,
I'm creating a dataview "on the fly" in order to sort some data prior
to writing out the information to a MS SQL table.
I have used two methods in order to determine the sort order of the
DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, in
Window2K server"). First of all, here are the two methods I have used in
order to apply the sorting property to the DataView.
1. Simply defining the sort order and column
DataView dvReturn;

string szSort = "DataRowID" + " ASC";
vReturn.Sort = szSort;

2. Defining a primary key and then setting the default sort order
property
/// when creating the datatable first
DataTable dt;
dt.PrimaryKey = new DataColumn[] {dt.Columns["DataRowID"]};
DataView dvFinal = new DataView(dt);

// then set the property
dvFinal.ApplyDefaultSort = true;

When I attempt to access the data from the DataView, the information
is still listed as if it hasn't been sorted. Here is a sample of the code I
have implemented in order to retrieve the sorted information (dvReturn would
be the DataView I applied the sorting properties to)
int iRowID = System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString());
int iEmpStatusID = System.Convert.ToInt32(dvReturn[j]["EmpStatusTypeID"].ToString());

Any help would be greatly appreciated, as it is not obvious to me what
I am doing wrong. Thank you in advance.
 
G

Guest

William
1. Apologies for the ambiguity.... I have not used the two sorting mechanisms concurrently. I am aware if you set the sort order initially (as in item #1) and then set the "ApplyDefaultSort" property to "true", that it will overwrite the inital sort ou specificed.... (it is clearly stated in the documentation)... but thank you anyway

2. The last reference (int iRowID = System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString()) is referencing a single row, where "j" is simply an iterator within a "for" loop. I am copying the contents of each row within the DataView back to an ArrayList.

I am interested in your comment re: "j" though... how will "j" determine my sort order... ?? Please respond at your convenience, and thank you for the assistance

Regards
jwede


----- William Ryan eMVP wrote: ----

Jwedel

If you call ApplyDefaultSort after the sort, it will undo the sort yo
specified and use the original one. What exactly is that last referenc
doing? It looks like you are just referencing one row, so whatever J i
will determine the sort order. Nonetheless, if you just use Sort like yo
did in the first one, regardless of primary key, you'll sort on that field
jwedel_stolo said:
Hi
I'm creating a dataview "on the fly" in order to sort some data prio
to writing out the information to a MS SQL table
I have used two methods in order to determine the sort order of th
DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, i
Window2K server"). First of all, here are the two methods I have used i
order to apply the sorting property to the DataView
1. Simply defining the sort order and colum DataView dvReturn
string szSort = "DataRowID" + " ASC" vReturn.Sort = szSort
2. Defining a primary key and then settin
the default sort orde
propert
/// when creating the datatable firs
DataTable dt
dt.PrimaryKey = new DataColumn[ {dt.Columns["DataRowID"]}
DataView dvFinal = new DataView(dt)
// then set the propert
dvFinal.ApplyDefaultSort = true
When I attempt to access the data from the DataView, the informatio
is still listed as if it hasn't been sorted. Here is a sample of the code
have implemented in order to retrieve the sorted information (dvReturn woul
be the DataView I applied the sorting properties to
int iRowID = System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString())
int iEmpStatusID System.Convert.ToInt32(dvReturn[j]["EmpStatusTypeID"].ToString())
Any help would be greatly appreciated, as it is not obvious to me wha I am doing wrong. Thank you in advance
jwede
 
W

William Ryan eMVP

jwedel_stolo said:
William,
1. Apologies for the ambiguity.... I have not used the two sorting
mechanisms concurrently. I am aware if you set the sort order initially (as
in item #1) and then set the "ApplyDefaultSort" property to "true", that it
will overwrite the inital sort ou specificed.... (it is clearly stated in
the documentation)... but thank you anyway.

I'm lost then, how does it fit in here or is just extraneous code? When
does it get applied b/c the timing of the events with this could easily
explain the problem.
2. The last reference (int iRowID =
System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString()) is referencing a
single row, where "j" is simply an iterator within a "for" loop. I am
copying the contents of each row within the DataView back to an ArrayList..

We need to determine where exactly the problem is. So go ahead and grab the
last value of the datatable in Column0 for instance and see what it is.
Then apply the sort and reference that row within the View! Only the view
is sorted not the datatable. An easy way to do this is make a test grid and
bind to it. Have a button that changes the sort field. Click on it a few
times and make sure it's sorting correctly. You'll have a visual cue if you
use a grid. I've never seen this not work and I use it constantly so I
suspect it may appear that it's not sorting but it's probably the reference
to the row.
I am interested in your comment re: "j" though... how will "j"
determine my sort order... ?? Please respond at your convenience, and thank
you for the assistance.

Whatever j will pass that row regardless of how it's sorted. Basically, if
J is 200 and there are 200 rows, you'll get the last row, but after a sort,
if the same row is now position 15, it's only going to be 'correct' when j
is 15.
Regards,
jwedel



----- William Ryan eMVP wrote: -----

Jwedel:

If you call ApplyDefaultSort after the sort, it will undo the sort you
specified and use the original one. What exactly is that last reference
doing? It looks like you are just referencing one row, so whatever J is
will determine the sort order. Nonetheless, if you just use Sort like you
did in the first one, regardless of primary key, you'll sort on that field.
jwedel_stolo said:
Hi,
I'm creating a dataview "on the fly" in order to sort some data
prior
to writing out the information to a MS SQL table.
I have used two methods in order to determine the sort order of the
DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, in
Window2K server"). First of all, here are the two methods I have used in
order to apply the sorting property to the DataView.
and column
DataView dvReturn;
setting
the default sort order
property
/// when creating the datatable first
DataTable dt;
dt.PrimaryKey = new DataColumn[] {dt.Columns["DataRowID"]};
DataView dvFinal = new DataView(dt);
// then set the property
dvFinal.ApplyDefaultSort = true;
When I attempt to access the data from the DataView, the
information
is still listed as if it hasn't been sorted. Here is a sample of the code I
have implemented in order to retrieve the sorted information (dvReturn would
be the DataView I applied the sorting properties to)
int iRowID =
System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString());
int iEmpStatusID = System.Convert.ToInt32(dvReturn[j]["EmpStatusTypeID"].ToString());
Any help would be greatly appreciated, as it is not obvious to
me what
I am doing wrong. Thank you in advance.
 
G

Guest

Willia
Thanks for all your help. As it turns out, the implementation of the sort and the subsequent accessing of the data is correct. I'm sorting on an integer value. Therefore, if I had an array of integers such as 1,2,13,3... the subsequent sort order will be: 1,13,2,3... I had to preceed the values with a '0' in order to obtain the correct sort order I desired....
(ie; 01, 02, 03, 13... is the sort order I requried).... apologies for wasting your time

regards
jwede

...
Thanks in advance

----- William Ryan eMVP wrote: ----



jwedel_stolo said:
William
1. Apologies for the ambiguity.... I have not used the two sortin
mechanisms concurrently. I am aware if you set the sort order initially (a
in item #1) and then set the "ApplyDefaultSort" property to "true", that i
will overwrite the inital sort ou specificed.... (it is clearly stated i
the documentation)... but thank you anyway

I'm lost then, how does it fit in here or is just extraneous code? Whe
does it get applied b/c the timing of the events with this could easil
explain the problemSystem.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString()) is referencing
single row, where "j" is simply an iterator within a "for" loop. I a
copying the contents of each row within the DataView back to an ArrayList.

We need to determine where exactly the problem is. So go ahead and grab th
last value of the datatable in Column0 for instance and see what it is
Then apply the sort and reference that row within the View! Only the vie
is sorted not the datatable. An easy way to do this is make a test grid an
bind to it. Have a button that changes the sort field. Click on it a fe
times and make sure it's sorting correctly. You'll have a visual cue if yo
use a grid. I've never seen this not work and I use it constantly so
suspect it may appear that it's not sorting but it's probably the referenc
to the row
determine my sort order... ?? Please respond at your convenience, and than
you for the assistance

Whatever j will pass that row regardless of how it's sorted. Basically, i
J is 200 and there are 200 rows, you'll get the last row, but after a sort
if the same row is now position 15, it's only going to be 'correct' when
is 15
Regards jwede
Jwedel
If you call ApplyDefaultSort after the sort, it will undo the sor
yo
specified and use the original one. What exactly is that las referenc
doing? It looks like you are just referencing one row, so whatever i
will determine the sort order. Nonetheless, if you just use Sort lik yo
did in the first one, regardless of primary key, you'll sort on tha field
jwedel_stolo said:
Hi
I'm creating a dataview "on the fly" in order to sort some dat
prio
to writing out the information to a MS SQL table
I have used two methods in order to determine the sort order of th
DataView. (I'm writing in C# with the v1.1.4322 of the .NE Framework, i
Window2K server"). First of all, here are the two methods I have use i
order to apply the sorting property to the DataView
and colum
DataView dvReturn
setting
the default sort order
property
/// when creating the datatable first
DataTable dt;
dt.PrimaryKey = new DataColumn[] {dt.Columns["DataRowID"]};
DataView dvFinal = new DataView(dt);
// then set the property
dvFinal.ApplyDefaultSort = true;
When I attempt to access the data from the DataView, the
information
is still listed as if it hasn't been sorted. Here is a sample of the code I
have implemented in order to retrieve the sorted information (dvReturn would
be the DataView I applied the sorting properties to)
int iRowID =
System.Convert.ToInt32(dvReturn[j]["DataRowID"].ToString());
int iEmpStatusID = System.Convert.ToInt32(dvReturn[j]["EmpStatusTypeID"].ToString());
Any help would be greatly appreciated, as it is not obvious to
me what
I am doing wrong. Thank you in advance.
 

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