Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an UI with a list view control on it that loads several thousand lines of data across about 50 columns. This is easy to export to Excel directly from the UI since the ListView lives on the UI thread, but I would like to create a separate thread to export the ListView data to an Excel file so if it is taking to long, I can kill the thread. How do I retrieve the List View Item from the main thread within the worker thread in C#?
C#
for (int i = 0; i < MaxI; i++)
{
    row = i + 2;
    ExcelSheet.Cells[row, 1] = myListView.Items[i].SubItems[0].Text;
    ExcelSheet.Cells[row, 2] = myListView.Items[i].SubItems[1].Text;
    ExcelSheet.Cells[row, 3] = myListView.Items[i].SubItems[2].Text;
    ExcelSheet.Cells[row, 4] = myListView.Items[i].SubItems[3].Text:
   ...
}


What I have tried:

I tried to create a delegate to pull back just the list view item text passing in the "i" and SubItem index, but I am doing something wrong createing the delegate or in my invoke. Has anyone else worked to export a List View into an Excel in a worker thread?
Posted
Updated 14-Oct-22 3:39am
v2

You don't. You cannot touch a control from any other thread other than the UI (startup) thread.

You're using the ListView control as a container for data instead of its intended purpose of displaying and editing data. You should be holding all that data in a container class dedicated to that purpose. That way, the control can display the data and you can even pass that data to another thread for it to work on, like writing it to an export file.
 
Share this answer
 
To add to what Dave has - rightly - said: you should never throw "thousands of lines" at a user anyway: it's slow, inefficient, and for the user completely useless - how long do you think it's going to take a user to find the row he is interested in buried in several thousand rows of 50+ columns?

Would you want to use it? If I was presented with that, I'd uninstall your app, and be demanding my money back ...

Show the user a max of about 100 items: page it, search it, filter it: don't just throw data at them and expect them to do the hard work every time they use your app!
 
Share this answer
 
Thank you both, but I have found the solution to my issue. However, to give some background to the context, the Federal Government releases a PDF document (and it is ONLY available as a PDF document) of a list of about 20k financial products. Financial companies are then expected to parse this PDF document, match it to their financial products sold by an unique identifier, calculate the total amount of assets being held at for financial product, add this amount into a particular column for the given product (and calculate as well based on the "family" of productions) and report all of this data back in a designated XML output. There is a lot more going on here that I will not waste your time explainng. However, no file being parsed is ever perfect when there is "no uniform method of entry". A company could be sending "INI ALC 2" and splitting after the IDI, another after the ALC, another before the INI and as mentioned, the name must be exact to what the government is supplying (include the split column). I use ML so as it is updated, it remembers by company and will apply it's learning to other products of the same company; however, it still requires a manual review for "new" companies added. Thus, the List View. I have restricted it to just new records and other variations in the past, but compliance want to see the entire file each time. Dumping from the List View to XML takes about 10 ms. But an XML is useless to a compliance person. They want it as an Excel. However, the "actual" container is a database table so I just changed my thread to pull the data directly from the DB table when the Excel output button is clicked and I have no more issues. They can cancel the thread when needed now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900