Home All Groups Group Topic Archive Search About

Datalist/datatable sorting

Author
14 Mar 2005 2:07 AM
Brian Barnes
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

Author
14 Mar 2005 10:22 AM
Cor Ligthert
Brian,

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

1   EmailAdr***@wathever.com
2   EmailAdr***@wathever.com


\\\
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) = "EmailAdr***@wathever.com"
dt.Rows(1)(1) = "EmailAdr***@wathever.com"
dt.DefaultView.Sort = "Id"
dataList1.DataSource = dt.DefaultView
dataList1.DataBind()
///

I hope this helps?

Cor
Author
15 Mar 2005 1:14 AM
Brian Barnes
Show quote Hide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:ONm1o%23HKFHA.4028@tk2msftngp13.phx.gbl...
> Brian,
>
> I tested your problem. The code beneath gave me a datalist what looks like
> this.
>
> 1   EmailAdr***@wathever.com
> 2   EmailAdr***@wathever.com
>
>
> \\\
> 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) = "EmailAdr***@wathever.com"
> dt.Rows(1)(1) = "EmailAdr***@wathever.com"
> 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 :-(
Author
15 Mar 2005 6:47 AM
Cor Ligthert
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
Author
29 Mar 2005 4:07 AM
Brian Barnes
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:u5ovOrSKFHA.1948@TK2MSFTNGP14.phx.gbl...
> 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