Solved

datagridview to CSV how to use this solution?

Posted on 2008-10-24
4
748 Views
Last Modified: 2013-12-17
The solution given to this question http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23263374.html?sfQueryTermInfo=1+c+datagridview+export seems to be almost exactly what i have been trying to achieve.

But what value do i pass it when i call it and how. the        List<int>    what does that refer to? Im assuming i have to build a List of something from the DataGridView, but not sure how
0
Comment
Question by:A_Kardas
  • 2
  • 2
4 Comments
 

Author Comment

by:A_Kardas
ID: 22797848
Sorry just realized how foolish this question actually is. Too many hours staring confused at the screen.

0
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 200 total points
ID: 22797897
In the given sample List<int> is a list of row numbers that need not be exported to the csv file. If you want to avoid exporting certain rows to csv file you have to pass the index of the rows. If you want to export all rows you can use the code below. Did few changes to original code.

void ExportGridData()
        {
            StreamWriter writer = new StreamWriter(@"D:\My_Documents\GridExport.txt");
 
            if (dataGridView1.Rows.Count > 0)
            {
                foreach (DataGridViewColumn col in dataGridView1.Columns)
                {
                    if (col.Index == dataGridView1.Columns.Count - 1)
                    {
                        writer.WriteLine(col.HeaderText);
                    }
                    else
                    {
                        writer.Write(string.Concat(col.HeaderText, ","));
                    }
                }
 
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                        foreach (DataGridViewCell cell in row.Cells)
                        {
                            if (cell.OwningColumn.Index == dataGridView1.Columns.Count - 1)
                            {
                                if (cell.Value != null)
                                    writer.WriteLine(cell.Value.ToString());
                                else
                                    writer.WriteLine("");
                            }
                            else
                            {
                                if (cell.Value != null)
                                    writer.Write(string.Concat(cell.Value.ToString(), ","));
                                else
                                    writer.Write(string.Concat("", ","));
                            }
                        }                    
                }
            }
 
            writer.Close();
        }
0
 

Author Closing Comment

by:A_Kardas
ID: 31509708
Yep of course, thank you just learning c# and programming in general. Thank you very much for the assistance. Im amazed at how great this site is. I will be signing up for atleast a year to get my skills up to speed!
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22798516
You are welcome
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question