Copying ListView data to the clipboard

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());

Tags: , , , , ,

Programming

Comments are closed

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011