Solved

datagridview to CSV how to use this solution?

Posted on 2008-10-24
4
743 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now