2016-08-19 44 views
-1

listView1 Example如何從一個字符串在C#

我需要知道,如果有一種方式來獲得COLUMN2某一個項目成一個字符串作爲文本值一定的文本值。

我的想法是鍵入學生textBox1之一的名稱,並通過點擊Button1它會得到textBox1輸入學生姓名右側的年齡,將其放入textBox2或在string

我在C# [圖片作爲示例]

+0

請提供更多信息,您如何填充ListView?你到目前爲止做了什麼 –

+0

到目前爲止你做了什麼 –

回答

0
public class PersonRecord 
{ 
    public string Name { get; set; } 
    public string AgeText { get; set; } 
    public int Age {get;set;} // ideally you wanna use integer to store person's age 
} 

public IEnumerable<string> GetAgeValue(string searchName) 
{ 
    var records = new List<PersonRecord>() 
    { 
     new PersonRecord {Name="David", AgeText="22 years old", Age = 22 }, 
     new PersonRecord {Name="Sarah", AgeText="23 years old", Age = 23 }, 
     new PersonRecord {Name="Jane", AgeText="24 years old", Age = 24 }, 
    }; 
    // assume it follows the strict format, "[age] years old", so we will get the age value based on the first occurrence of the white space 
    var result = records.Where(x => x.Name.Equals(searchName, StringComparison.OrdinalIgnoreCase)).Select(x => x.AgeText.Substring(0, x.AgeText.IndexOf(" ") + 1)); 
    // alternatively just do a query on the age directly, much easier 
    // var result = records.Where(x => x.Name.Equals(searchName, StringComparison.OrdinalIgnoreCase)).Select(x => x.Age.ToString()); 
    return result; 
} 

使用listview總之,以上僅僅是用於獲取人的年齡一個快速的代碼示例,並希望這將讓你開始。

相關問題