Saturday, June 26, 2010

Exporting DataGridView to CSV in C#

Wrote this subroutine for my WINFORMS application. I had previously written this subroutine for Web and have found limited resources on the NET for it. This should allow you to export All Rows and Selected Rows.


private void exportToCSV(DataGridView dataGridViewResults, string FileName1)
        {
            try
            {
                StreamWriter sw = new StreamWriter(FileName1);
                
                if (dataGridViewResults.SelectedRows.Count > 2)
                {


                    for (int j = 0; j < dataGridViewResults.Columns.Count; j++)
                    {
                        sw.Write(dataGridViewResults.Columns[j].HeaderText + ",");
                    }
                    sw.WriteLine();
                    foreach (DataGridViewRow row in dataGridViewResults.SelectedRows)
                    {
                        for (int j = 0; j < dataGridViewResults.Columns.Count; j++)
                        {
                            //MessageBox.Show(row.Cells[29].Value.ToString());
                           sw.Write(row.Cells[j].Value.ToString() + ",");
                        }
                        sw.WriteLine();
                    }


                    sw.Flush();
                    sw.Close();


                    txtToolRPK.Text = "Successfully saved CSV: " + FileName1 + " for: " +
                   txtMachineName.Text + " - [" + 
                   dataGridViewResults.SelectedRows.Count.ToString() + 
                   "] processes.";
                    ntyRPK.ShowBalloonTip
                             (5000,
                             appTitle,
                             "Successfully saved CSV: " + FileName1 + " for: " +
                             txtMachineName.Text + " - [" +
                             dataGridViewResults.SelectedRows.Count.ToString() + 
                             "] processes.",
                             ToolTipIcon.Info);


                }
                else
                {
                    for (int j = 0; j < dataGridViewResults.Columns.Count; j++)
                    {
                        sw.Write(dataGridViewResults.Columns[j].HeaderText + ",");
                    }
                    sw.WriteLine();


                    for (int i = 0; i < 
                        dataGridViewResults.Rows.Count;
                         i++)
                    {
                        for (int j = 0; j < dataGridViewResults.Columns.Count; j++)
                        {
                            try
                            {
                                sw.Write(
                                    dataGridViewResults[j, i].Value.ToString() + ",");
                            }
                            catch
                            {}
                        }
                        sw.WriteLine();


                    }
                 
                    sw.Flush();
                    sw.Close();


                    txtToolRPK.Text = "Successfully saved CSV: " + FileName1 + " for: " +
                  txtMachineName.Text + " - [" + dataGridViewResults.Rows.Count.ToString() + 
                  "] processes.";
                    ntyRPK.ShowBalloonTip
                             (5000,
                             appTitle,
                             "Successfully saved CSV: " + FileName1 + " for: " +
                             txtMachineName.Text + " - [" +
                             dataGridViewResults.Rows.Count.ToString() + "] processes.",
                             ToolTipIcon.Info);
                }
              
               
            }
            catch (Exception ex)
            {
                txtToolRPK.Text = "Error saving CSV: " + FileName1 + " for: " +
                         txtMachineName.Text;
                ntyRPK.ShowBalloonTip
                         (5000,
                         appTitle,
                         "Error saving CSV: " + FileName1  + " for: " +
                         txtMachineName.Text + "-" + ex.Message,
                         ToolTipIcon.Warning);
            }
        }


No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...