Tuesday, January 13, 2009

Copying Listview data to ClipBoard C#

Its pretty interesting working around with a listview. Most of the time we use export to excel functionality to copy the list item.In this case we might be required to take care of the version of Excel and all those complex stuff.Clip board is a cool solution for this problem . so lets find out how we can copy selected items in a list view to Clipboard.



string strToClipbrd=""; //stores the items in list view
foreach (ColumnHeader columnHeader in listview.Columns)
{
strClipBrd = strClipBrd + columnHeader.Text+"\t"; // \t help us to navigate to nextcell
}
strClipBrd = strClipBrd + columnHeader.Text+"\n"; // to the new line or to the next row
for (int i = 0; i < listview.Items.Count; i++)
{
for (int j = 0; j < listview.Columns.Count; j++)
{
if (listview.Items[i].Selected == true) //only selected rows are allowed
strToClipbrd= strToClipbrd+ listview.Items[i].SubItems[j].Text + "\t";
}
strToClipbrd= strToClipbrd+ "\n";
}
Clipboard.SetDataObject(strToClipbrd, true);

No comments:

Post a Comment