1

I was wondering if there was a way to format 2 strings in a selectlist and 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.

4

3 に答える 3

1

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);
}

結果として

Item0     |Name0
Item5     |Name5
Item10    |Name10

もちろん、ドロップダウン リストで固定幅フォントを使用していることを確認する必要があります。

于 2009-11-12T19:35:42.477 に答える
1

文字列形式を使用して、アイテムのテキストを作成できます。

string itemstring = string.Format("Item {0:0000000000}|Name {0}", itemNumber);

データ バインディングを使用してアイテムを作成する場合は、フォーマット式を DataTextFormatString に配置して、ASP.NET でアイテムをフォーマットすることができます。

于 2009-11-12T19:46:03.783 に答える
0

それをフォーマットする最良の方法は次のとおりです。

string itemstring = String.Format("{0,-10}|{1}", item, name);
于 2009-11-12T21:16:56.547 に答える