2016-08-19 46 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总之,以上仅仅是用于获取人的年龄一个快速的代码示例,并希望这将让你开始。

相关问题