dataview sorting in manual order?

J

Jason

Is it possible to somehow sort a dataview, all on one column, but on
different variables?

That is, I have a 'Title' column in my dataview and I'd like to display
first all "Directors" (sorted), then everyone else (sorted). I've used the
'ItemBound' but can't figure out how to do this with that procedure.

Thanks in advance, any ideas are appreciated.

Jason

eg of what I'm looking for:

Name Title
===== ======
George Director
Yvonne Director
Harry Programmer
Larry Research Officer
Wilbur Secretary
 
G

Guest

Right after you bind the dataview, do the following:

dataview.Sort = "Titles ASC, Directors ASC"
 
J

Jason

Unfortunately, I get:

Exception Details: System.IndexOutOfRangeException: Cannot find column
Director.

Source Error:

Line 197:
Line 198: DGADResults.DataSource = dvCustom
Line 199: dvCustom.Sort = "title ASC, Director ASC"
Line 200: DGADResults.DataBind()
Line 201: End Sub


thoughts?
 
S

Scott Allen

Jason:

You don't have a column named "Director". Change the Sort to only use
columns you do have, like Title and Name.

dataview.Sort = "Title ASC"
 
G

Guest

I guess it has to be

DGADResults.DataSource = dvCustom
dvCustom.Sort = "title ASC, Name ASC"
DGADResults.DataBind()
 
J

Jason

First off, thanks to everyone who's responded, I think we're almost there:

Alright, so I've sorted dataviews before by a column name like this without
a problem. What I'm trying to get however is a sorted list of all rows
whose column 'title' has a value of "Director" -THEN- list the rest of the
rows in normal sorted order.

That is, just as in my example below, although Yvonne and George should not
be at the top of the list because their names fall alphabetically below
'Barry', because they have a title='Director' value, I would like them to be
on the top of the list first. Following all of these Directors, Barry,
Larry, and Wilbur are sorted normally.

Name Title
===== ======
George Director
Yvonne Director
Barry Programmer
Larry Research Officer
Wilbur Secretary

dataview.Sort = "Title ASC" won't cut it, nor would dvCustom.Sort = "title
ASC, Name ASC" because my data could contain a title='Associate something'
that would take the top spot instead of any "Director".
 
S

Scott Allen

Ah, thats a bit tricker. There is no built in way to do this easily
but here is something to try...

Add an additional column to the underlying DataTable. When the Title
is Director, set the value of this column to 1, when the Title is not
a director, set the value to 2. Then you could use a Sort expression
like:

dv.Sort = "SortColumn ASC, Name ASC";

The additional column could be a computed column. Add the column
programatically:

dataTable.Columns.Add("SortColumn", typeof(int), expr);

Where expr is an expression like:

"IIF(Title='Director', 1, 2)"


HTH,
 
J

Jason

Good idea, I'll give that a whirl and it should suit my needs just fine.

Thanks alot!

Jay
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top