PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Datalist/datatable sorting

 
 
Brian Barnes
Guest
Posts: n/a
 
      14th Mar 2005
Hi,
I know this has been done quite often before, but all my research and trials
have not worked. Basically I am trying to display a sorted list of e-mail
Addresses in a datalist control (using web forms) from a dataset. The only
table in the dataset has 2 columns (UserID (Guid (defined as a string),
primary Key) and UserEmail (string)) and is filled from an Access database.

It is not practical to bring the data in sorted (although it is possible) as
it makes the SQL statement "hairy" (and I like to write clean code). Also I
doubt the speed gain (if any) will be negligible due to the small size I
expect the table to be.

My current code is:

datGroupAdmin.tblUser.DefaultView.Sort = "UserEmail"
lstGroupAdmin.DataSource = datGroupAdmin.tblUser
lstGroupAdmin.DataBind()

and does not show the list sorted.

I also have tried:
datGroupAdmin.tblUser.DefaultView.Sort = "UserEmail"
lstGroupAdmin.DataSource = datGroupAdmin.tblUser.DefaultView
lstGroupAdmin.DataBind()

again to no avail.

I have also tried implementing my own sort method:
tblClone = tblTest.Copy
tblTest.Clear()

While tblClone.Rows.Count > 0
rowLow = tblClone.Rows(0)
For Each row In tblClone.Rows
If row.Item(0).ToString.ToLower < rowLow.Item(0).ToString.ToLower
Then
RowLow = row
End If
Next
tblTest.Rows.Add(rowLow)
tblClone.Rows.Remove(row)
End While

but this came up with a runtime error "5" "This row already belongs to
another table" (I realise that this isn't the best sort method, it was a
test to see if I could get it to work).

At one stage, I believe I had the datatable sorted (but this did not display
sorted in the datalist - I'm sure I don't re-initialise the datatable or
datalist elsewhere).

--
Brian Barnes


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      14th Mar 2005
Brian,

I tested your problem. The code beneath gave me a datalist what looks like
this.

1 (E-Mail Removed)
2 (E-Mail Removed)


\\\
Dim dt As New DataTable
dt.Columns.Add("Id")
dt.Columns.Add("EmailAdres")
dt.Rows.Add(dt.NewRow)
dt.Rows.Add(dt.NewRow)
dt.Rows(0)(0) = "2"
dt.Rows(1)(0) = "1"
dt.Rows(0)(1) = "(E-Mail Removed)"
dt.Rows(1)(1) = "(E-Mail Removed)"
dt.DefaultView.Sort = "Id"
dataList1.DataSource = dt.DefaultView
dataList1.DataBind()
///

I hope this helps?

Cor


 
Reply With Quote
 
Brian Barnes
Guest
Posts: n/a
 
      15th Mar 2005

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:ONm1o%(E-Mail Removed)...
> Brian,
>
> I tested your problem. The code beneath gave me a datalist what looks like
> this.
>
> 1 (E-Mail Removed)
> 2 (E-Mail Removed)
>
>
> \\\
> Dim dt As New DataTable
> dt.Columns.Add("Id")
> dt.Columns.Add("EmailAdres")
> dt.Rows.Add(dt.NewRow)
> dt.Rows.Add(dt.NewRow)
> dt.Rows(0)(0) = "2"
> dt.Rows(1)(0) = "1"
> dt.Rows(0)(1) = "(E-Mail Removed)"
> dt.Rows(1)(1) = "(E-Mail Removed)"
> dt.DefaultView.Sort = "Id"
> dataList1.DataSource = dt.DefaultView
> dataList1.DataBind()
> ///
>
> I hope this helps?


Unfortunately I had tried this (and listed this in my orignal post). It
wasn't sorting it by guid either. It did put me on the right track though.

On further inspection, I found that the default view was indeed sorted, and
that I had fields in the properties box in the form designer. I deleted
these (so now there were no databound variables set in the form designer)
and set the DataTextField & DataValue field in code. The final code (that
works!) is:

datGroupAdmin.tblUser.DefaultView.Sort = "UserEmail"
lstGroupAdmin.DataSource = datGroupAdmin.tblUser.DefaultView
lstGroupAdmin.DataValueField = "UserId"
lstGroupAdmin.DataTextField = "UserEmail"
lstGroupAdmin.DataBind()

All the more reason not to trust Microsoft's form designer. It is possible
to build web forms without it, but some of the code required to do this is
tedious (try building a html table in code ;-) - yes I have done it as part
of a page template).

--
Brian Barnes
Now to apply that code to all the other datalists :-(


 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      15th Mar 2005
Brian,

dt.DefaultView.Sort = "Id"
dataList1.DataSource = dt.DefaultView
dataList1.DataBind()

> Unfortunately I had tried this (and listed this in my orignal post). It
> wasn't sorting it by guid either. It did put me on the right track though.


I tested it, so that is strange that you did not saw that.

>
> datGroupAdmin.tblUser.DefaultView.Sort = "UserEmail"
> lstGroupAdmin.DataSource = datGroupAdmin.tblUser.DefaultView
> lstGroupAdmin.DataValueField = "UserId"
> lstGroupAdmin.DataTextField = "UserEmail"
> lstGroupAdmin.DataBind()
>


This is the same code I showed you? The only difference is that I have the
DataValueField and DataTextField in the HTML part because I was not using
the designer to get the dataset.

Cor


 
Reply With Quote
 
Brian Barnes
Guest
Posts: n/a
 
      29th Mar 2005

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Brian,
>
> dt.DefaultView.Sort = "Id"
> dataList1.DataSource = dt.DefaultView
> dataList1.DataBind()
>
>> Unfortunately I had tried this (and listed this in my orignal post). It
>> wasn't sorting it by guid either. It did put me on the right track
>> though.

>
> I tested it, so that is strange that you did not saw that.
>

Please note that the main cause of my error was not in code, but with the
desginer. I had tried something similar to your code (See orginal post) but
as the error was in the designer which I believe overwrote any code that I
had written. The reason why I said it put me on the right track is that I
knew I had tried the right code, so there must have been something else (the
designer as I later found out).
>>
>> datGroupAdmin.tblUser.DefaultView.Sort = "UserEmail"
>> lstGroupAdmin.DataSource = datGroupAdmin.tblUser.DefaultView
>> lstGroupAdmin.DataValueField = "UserId"
>> lstGroupAdmin.DataTextField = "UserEmail"
>> lstGroupAdmin.DataBind()
>>

>
> This is the same code I showed you? The only difference is that I have the
> DataValueField and DataTextField in the HTML part because I was not using
> the designer to get the dataset.
>

You are correct Cor, I am not faulting your code. My error was not in the
code, but in having extra in the designer (you have excluded the main part
of the solution from your post).

Unfortunately with VS, you now have to look at more than just the code you
write, bugs can appear in the designer as well.

--
Brian


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataList, DataSet or DataTable rows limit Aahz Microsoft C# .NET 5 10th Jun 2006 07:15 AM
Sorting a Datalist in ASP.net 2.0 LisaBigJax Microsoft ASP .NET 0 9th Jan 2006 02:45 AM
Sorting a Datalist in ASP.net 2.0 LisaBigJax Microsoft ASP .NET 0 8th Jan 2006 04:05 PM
Sorting a datalist Dufus Microsoft ASP .NET 2 24th Nov 2004 08:54 PM
sorting datalist in ASP.Net? LRW Microsoft Dot NET 1 30th Sep 2003 09:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:52 AM.