by Codewiz51
26. May 2010 21:53
Have you ever wanted to copy the data from a ListView control to the clipboard? The following code places the ListView data on the clipboard in a tab delimited format so it can be pasted into Excel. Hope you find it useful.
Clipboard.Clear();
StringBuilder buffer = new StringBuilder();
// Setup the columns
for ( int i = 0; i < this.lstMyListView.Columns.Count; i++ )
{
buffer.Append(this.lstMyListView.Columns [ i ].Text);
buffer.Append("\t");
}
buffer.Append("\n");
// Build the data row by row
for ( int i = 0; i < this.lstMyListView.Items.Count; i++ )
{
for ( int j = 0; j < this.lstMyListView.Columns.Count; j++ )
{
buffer.Append(this.lstMyListView.Items [ i ].SubItems [ j ].Text);
buffer.Append("\t");
}
buffer.Append("\n");
}
Clipboard.SetText(buffer.ToString());