473,569 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datalist/datatable sorting

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.t blUser.DefaultV iew.Sort = "UserEmail"
lstGroupAdmin.D ataSource = datGroupAdmin.t blUser
lstGroupAdmin.D ataBind()

and does not show the list sorted.

I also have tried:
datGroupAdmin.t blUser.DefaultV iew.Sort = "UserEmail"
lstGroupAdmin.D ataSource = datGroupAdmin.t blUser.DefaultV iew
lstGroupAdmin.D ataBind()

again to no avail.

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

While tblClone.Rows.C ount > 0
rowLow = tblClone.Rows(0 )
For Each row In tblClone.Rows
If row.Item(0).ToS tring.ToLower < rowLow.Item(0). ToString.ToLowe r
Then
RowLow = row
End If
Next
tblTest.Rows.Ad d(rowLow)
tblClone.Rows.R emove(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
Nov 21 '05 #1
3 4382
Brian,

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

1 Em*********@wat hever.com
2 Em*********@wat hever.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) = "Em*********@wa thever.com"
dt.Rows(1)(1) = "Em*********@wa thever.com"
dt.DefaultView. Sort = "Id"
dataList1.DataS ource = dt.DefaultView
dataList1.DataB ind()
///

I hope this helps?

Cor
Nov 21 '05 #2

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ON******** ********@tk2msf tngp13.phx.gbl. ..
Brian,

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

1 Em*********@wat hever.com
2 Em*********@wat hever.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) = "Em*********@wa thever.com"
dt.Rows(1)(1) = "Em*********@wa thever.com"
dt.DefaultView. Sort = "Id"
dataList1.DataS ource = dt.DefaultView
dataList1.DataB ind()
///

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.t blUser.DefaultV iew.Sort = "UserEmail"
lstGroupAdmin.D ataSource = datGroupAdmin.t blUser.DefaultV iew
lstGroupAdmin.D ataValueField = "UserId"
lstGroupAdmin.D ataTextField = "UserEmail"
lstGroupAdmin.D ataBind()

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 :-(
Nov 21 '05 #3
Brian,

dt.DefaultView. Sort = "Id"
dataList1.DataS ource = dt.DefaultView
dataList1.DataB ind()
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.t blUser.DefaultV iew.Sort = "UserEmail"
lstGroupAdmin.D ataSource = datGroupAdmin.t blUser.DefaultV iew
lstGroupAdmin.D ataValueField = "UserId"
lstGroupAdmin.D ataTextField = "UserEmail"
lstGroupAdmin.D ataBind()


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
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3077
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a label, a radiobuttonlist, etc. I'm driving myself insane trying to figure out how to get
2
1766
by: wh1974 | last post by:
I have a DataTable that I'm binding to a DataList control on my web page. I want to be able to restrict the number of rows that are initially displayed from the DataTable. For example I want to be able to show the first 5 rows, a button will then allow the user to 'show all records'. Although I've used a DataView before, I am unsure how this...
10
2829
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I have to add a datalist control. Using this datalist control I should be able to add edit, modify and cancel the items listed in this control. ...
6
1587
by: Patrick.O.Ige | last post by:
Hi guys, Just using these small snippet below using Datalist with sorting! But it appears i can see no Data on the screen when compiled with Visual Studio .Net! With WebMatrix its running fine!:) Whats missing in VStudio .Net? Imports System.Data.SqlClient
7
3082
by: tshad | last post by:
Is there a way to move a row in a Datalist up or down without having to re-read the data? I have a datalist which has embedded Datagrids in it. I want to allow the user to move a row up or down by just pushing a button and have the page post back with the row moved without actually re-reading the data from the database? Thanks,
0
1692
by: bryanp10 | last post by:
I have a DataList on my page which contains multiple DropDownLists. My page defaults to showing six rows in the DataList. I want the ability to dynamically add rows if needed. Right now I'm just using an empty table with X rows, and adding a new row to the table that the DataList is bound to. However, I can't figure out a way to do this and...
1
1911
by: Jim Heavey | last post by:
First time trying to use the Datalist and I can not seem to get the events to fire. I set up my Data List control and I identify the event for the "OnEditCommand". I also set up an "<ItemTemplate>" in which I place a ASP:Button control in and I specify the "CommandName" property for this button to be "edit". I ensure that my control has...
6
9571
by: Paul | last post by:
I am trying to use a DataList and the ItemTemplate. I am binding the Datalist to a SQL query that gives me a list of Items with a Parent Category. I want to loop through all the items, but only print the Parent Category once, regardless of how many child items are in it. So my perfect output would look something like:
2
3427
by: scottls | last post by:
Hi All, Thanks for reading my post. I have been working on getting nested datalists working properly wihtin my framework for many days and I think I'm almost there. See if you could help me get over the hump here. Some of our products come in different sizes - i.e. what I call "units". Some products don't. I successfully created the...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.