I was wondering if there was a way to format 2 strings in a selectlist an开发者_JAVA百科d display them as following:
String begins with Item1 and spaces following it until 10 spaces are taken up followed by a Delimiter "|" and string 2
So all Selectlist items binded to a drop down list will be displayed as following
Item 1 |Name1
Item 2 |Name2
Item 55 |Name3
Item 500 |Name4
Item 100000|Name5
Thanks in advance.
You can use string format to build the text for the items:
string itemstring = string.Format("Item {0:0000000000}|Name {0}", itemNumber);
If you are using data binding to build the item, you can put the format expression in the DataTextFormatString to have ASP.NET format the items for you.
You can use System.String.PadRight()
for(int i = 0; i <= 10; i+= 5)
{
string ItemString = "Item" + i.ToString().PadRight(10, ' ') + "|" + "Name" + i.ToString();
SelectList.Items.Add(ItemString);
}
would result in
Item0 |Name0
Item5 |Name5
Item10 |Name10
Of course, you'll want to ensure that you're using a fixed-width font in the drop-down list
best way to format it is:
string itemstring = String.Format("{0,-10}|{1}", item, name);
精彩评论