Pergunta

I have list of maybe 50,000 entries that are populated in datagrid in wpf. Now I want to save the data in the list to a file that may be text, or preferably CSV. As list is too big. There is a problem that my implemented method that may be simple text file writing or the method to copy the contents from the datagrid to clipboard and then back to string, and then that string to file using StreamReader. It consumes approx 4-5 minutes even it is in background worker.

Is there any way that I can save huge list to file quickly?

I am using DataGrid in WPF

CODE

 dataGrid1.SelectAllCells();
            dataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            ApplicationCommands.Copy.Execute(null, dataGrid1);
   String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);


///Never reach to step Below thread stays on above line
                dataGrid1.UnselectAllCells();
                Clipboard.Clear();
                StreamWriter file = new System.IO.StreamWriter(SavePageRankToPDF.FileName);
                file.WriteLine(result);
                file.Close();
Foi útil?

Solução

Instead of using the clipboard, why not iterate through the datatable and build the csv file.

Update

Here are some examples:

Convert DataTable to CSV stream

Converting DataSet\DataTable to CSV

Outras dicas

One thing that will help is to not load ALL of your data into the datagrid when using it for display purposes. It'd be a good idea to use paging: only load the data into the datagrid that will be needed for calculations or display purposes. If the user wants to see/use more data, go back to your data source and get more of the data. Not only will your app run faster, you'll use much less memory.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top