Multiple columns in List box control
Solution 1
A list box wasn't designed to display multi-column data. Even the Windows Forms version doesn't directly support that kind of data display.
Your requirements aren't clear, but the simplest way to go would be to use a GridView control. It gives you a lot of functionality out of the box, and you can expand it to more columns very easily. If you need more control over the look or functionality, you can use a DataList instead.
To get the scrolling ability, you can either use a scrolling <div>
or simply use the pagination mechanism of the GridView if that's appropriate.
Solution 2
You could line it up as if the data was in 2 columns
new ListItem("blah1".PadRight(10, ' ') + "blah2");
as shown here: http://articles.dotheweb.net/post/Formatting-columns-in-a-ListBox-of-ComboBox.aspx
Also, you could roll your own with a DataList.
Solution 3
If you want to use columns in a ListBox, you have to do it based on alignment.
For example:
String columns = "{0, -55}{1, -35}{2, -35}";
ListBox1.Items.Add(String.Format(columns, "Filename", "Selected DateModified", "Vault DateModified"));
ListBox1.Items.Add(String.Format(columns, fileName, datetime1, datetime2));
Output of my own implementation of this code below:
Keep in mind the font you use has to be a monospaced font, otherwise the alignment will mess up due to variable spacing between characters (and this exaggerates the longer the string is).
Solution 4
Looks like you should write your own control, or you can use the listview control.
Solution 5
use list view it is perfect alternative for multi column list box
Related videos on Youtube
xrx215
Updated on January 06, 2021Comments
-
xrx215 about 3 years
How can I display two columns in a list box?
-
KMån almost 14 years@xrx215: If the only intention is to display the data column wise - meaning no user interaction - then you can add spaces to render data as columns; just thinking...!
-
-
xrx215 almost 14 yearsthere is no such control as mulicolumn list box
-
KMån almost 14 years@xrx215: If you go through the article you would learn that
MultiColumnListBox
is a subclassed control. >This is an Owner Drawn ListBox inherited fromSystem.Windows.Forms.ListBox
. Its primary function is to format each Item into multiple columns. Which is what others have suggested. -
user229044 almost 14 yearsQuestion is tagged asp.net, link isn't relevant.
-
mckenzm about 2 years@xrx215, not in HTML, but what we call a list box is actually a Drop Down Combo Box (DDCB) used as an alternative to either radio buttons (single, mutually exclusive choice) or check boxes (mutiple choices), Almost all other GUI has true multi-column list boxes with paging and scroll bars. In HTML we are stuck with with the PHPMyAdmin style.