반응형
목록 시작 부분에 항목을 추가하려면 어떻게 해야 합니다.
[ Select One ]옵션을 드롭다운목록에 추가하여List<T>
.
에 대해 문의하면List<T>
이니셜을 추가하려면Item
데이터 소스의 일부가 아닌, 데이터 소스의 첫 번째 요소로List<T>
? 나는 다음을 가지고 있다:
// populate ti from data
List<MyTypeItem> ti = MyTypeItem.GetTypeItems();
//create initial entry
MyTypeItem initialItem = new MyTypeItem();
initialItem.TypeItem = "Select One";
initialItem.TypeItemID = 0;
ti.Add(initialItem) <!-- want this at the TOP!
// then
DropDownList1.DataSource = ti;
삽입 방법을 사용합니다.
ti.Insert(0, initialItem);
.NET 4.7.1 이후로는 부작용 없이및 을 사용할 수 있습니다.출력은 IEnumerable이 됩니다.
// Creating an array of numbers
var ti = new List<int> { 1, 2, 3 };
// Prepend and Append any value of the same type
var results = ti.Prepend(0).Append(4);
// output is 0, 1, 2, 3, 4
Console.WriteLine(string.Join(", ", results));
편집:
지정된 목록을 명시적으로 변환하는 경우:
// Creating an array of numbers
var ti = new List<int> { 1, 2, 3 };
// mutating ti
ti = ti.Prepend(0).ToList();
하지만 그 시점에서는
업데이트: "AppendDataBoundItems" 속성을 true로 설정한 다음 "Choose item"을 선언합니다.데이터 바인딩 작업은 정적으로 선언된 항목에 추가됩니다.
<asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="Please choose..."></asp:ListItem>
</asp:DropDownList>
- 오이신
삽입 방법 사용:List<T>
:
List.Insert 메서드(Int32, T):
Inserts
에 있는 목록에 있는 요소specified index
.
var names = new List<string> { "John", "Anna", "Monica" };
names.Insert(0, "Micheal"); // Insert to the first element
사용하다List<T>.Insert
구체적인 예와는 관련이 없지만 퍼포먼스가 중요한 경우LinkedList<T>
아이템을 첫머리에 삽입하기 때문에List<T>
모든 항목을 이동해야 합니다.자세한 내용은 목록과 LinkedList를 사용해야 하는 경우를 참조하십시오.
언급URL : https://stackoverflow.com/questions/390491/how-to-add-item-to-the-beginning-of-listt
반응형
'sourcetip' 카테고리의 다른 글
bash 스크립트에서 set -e는 무엇을 의미합니까? (0) | 2023.04.13 |
---|---|
RGB 색상을 가변적으로 저장하는 방법은 무엇입니까? (0) | 2023.04.13 |
WPF에서 그리드 제어에 경계를 설정하려면 어떻게 해야 합니까? (0) | 2023.04.13 |
인수를 사용하여 bash에서 Python 스크립트를 호출합니다. (0) | 2023.04.13 |
EF5: 파일 '{0}'을(를) 데이터베이스 '{1}'로 첨부할 수 없습니다. (0) | 2023.04.13 |