2013-06-27 130 views
0

我正在使用多行文本框和listview的C#应用​​程序。从文本框到列表视图

文本框的东西看起来是这样的:

John Smith 
Joe Bronstein 
Susan Jones 
Adam Feldman 

ListView中有两列:DateName

到目前为止,我可以将当​​前日期放入listview的Date列中。接下来,我需要将名称复制到名称列中。该listview应该是这样的:

Date  Name  
6/27/2013 John Smith 
6/27/2013 Joe Bronstein 
6/27/2013 Susan Jones 
6/27/2013 Adam Feldman 

那么,如何从textbox每一行副本的名称为Name栏上的listview每一行?

回答

2

这会从文字框添加所有名的ListView与当前日期:

var date = DateTime.Now.ToShortDateString(); 
foreach (var line in textBox.Lines) 
    listView.Items.Add(new ListViewItem(new string[] { date, line})); 

它是如何工作:我们列举TextBox财产Lines它通过行返回的名称一致。对于每行为ListView中的每列创建新的ListViewItem以及字符串数组。然后item添加到listView。

0

Lazyberezovsky答案完美。

但是,如果你在你的Listview已经增加了一个项目,要行的事实,你已经添加了Dates(老实说,我怀疑这只是一个猜测)之后添加。然后,您需要使用SubItem将每行添加到新列。现在,鉴于当然,你ListView相同数量的Items在你MultilineTextboxLines的。

所以,你的代码可能会是这样的:

string[] line = textBox1.Lines; // get all the lines of text from Multiline Textbox 
int i = 0; // index for the array above 
foreach (ListViewItem itm in listView1.Items) // Iterate on each Item of the ListView 
{ 
    itm.SubItems.Add(line[i++]); // Add the line from your textbox to each ListViewItem using the SubItem 
} 

否则再次Lazyberezovsky的回答完美的作品和正确的解决您的问题。

相关问题