How to create alphabetical order in asp.net data list column
I am trying to create a data list displaying the entire kitchen alphabetically in a column, however I could not find a way to do this, can anyone help.
using data list or relay in asp.net only allows display in horizontal or vertical alphabetical order like
Afghan Asian Burmese Cambodian
Chinese Croatian European French
Greek Indian International Italian
I want to have
Afghan Chinese Greek
Asian Croatian Indian
Burmese European International
Cambodian French Italian
thanks
Nickf's answer helped me navigate to the next method, which will modify the list as needed:
public static List<T> ReorderListIntoColumns<T>(List<T> list, int totalColumns)
{
List<T> reordered = new List<T>();
int itemsPerColumn = (int) list.Count / totalColumns;
for (int i = 0; i < itemsPerColumn; i++)
{
for (int j = 0; j < totalColumns; j++)
{
reordered.Add(list[(j * itemsPerColumn) + i]);
}
}
return reordered;
}
This question seems to be getting a lot of views, although no answer has been accepted. Hope this helps someone else who needs to reorder the list this way!
a source to share
What I would do is get the list in alphabetical order and then rebuild so that when you use the default order for datalist / repater it is in the correct format.
In fancy pseudocode.
/*
your input is A,B,C,D,E,F,G,H,I,J,K,L
given this order, it returns:
A, B, C
D, E, F
G, H, I
J, K, L
but you want
A, E, I
B, F, J
C, G, K
D, H, L
So you need to change your input to this:
A,E,I,B,F,J,C,G,K,D,H,L
*/
let numColumns = 3;
let numItems = 12;
let itemsPerColumn = numItems / numColumns;
loop i from 0 to itemsPerColumn - 1
loop j from 0 to numColumns - 1
newOrdering.push(oldOrdering[i * numColumns + j])
There might be some great .NET feature to change the ordering style, but this is just one old school technique.
PS: This works with the best scenario, which is that the number of items is evenly divisible by the number of columns. You will have to tweak a few values a little.
a source to share
I recently did this using a PlaceHolder
custom table as well:
<asp:PlaceHolder ID="locationTablePlaceHolder" runat="server"/>
Then in the backup code:
DataView view = locations.Tables[0].DefaultView;
view.Sort = "locationName";
Table table = new Table();
locationTablePlaceHolder.Controls.Clear();
locationTablePlaceHolder.Controls.Add(table);
// enumerator to loop through all items in the DataView
IEnumerator enumerator = view.GetEnumerator();
enumerator.Reset();
// first create all the row objects since we want to populate
// column by column.
int rowCount = 5;
int colCount = 5;
for (int i = 0; i < rowCount; i++)
{
TableRow row = new TableRow();
table.Rows.Add(row);
}
// then loop through each column taking items from the enumerator
// to populate the table
for (int j = 0; j < colCount; j++)
{
for (int i = 0; i < rowCount; i++)
{
TableCell cell = new TableCell();
if (enumerator.MoveNext())
{
Label label = new Label();
label.Text = (String)((DataRowView)enumerator.Current).Row["locationName"];
cell.Controls.Add(label);
table.Rows[i].Cells.Add(cell);
}
}
}