0

我正在开发一个带有多行文本框和listview.

文本框的内容如下所示:

John Smith
Joe Bronstein
Susan Jones
Adam Feldman

列表视图有两列:DateName

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

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

那么如何将名称从 中的每一行复制到 中每一行textboxName列中listview

4

2 回答 2

2

This will add all names from textBox to listView with current date:

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

How it works: We are enumerating TextBox property Lines which returns names line by line. For each line created new ListViewItem with array of strings for each column in your ListView. Then item is added to listView.

于 2013-06-27T21:46:28.110 回答
0

Lazyberezovsky回答完美。

但是,如果您已经在您的项目中添加了一个项目,Listview并且您想在您已经添加了之后添加这些行Dates(老实说,我对此表示怀疑,但这只是一个猜测)。然后,您需要使用SubItem将每行添加到新列。现在,当然,鉴于您ListView数量与您Items的. LinesMultiline Textbox

因此,您的代码可能如下所示:

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 的答案再次完美,并且是您问题的正确解决方案。

于 2013-06-28T01:08:42.817 回答