Solved

datagridview to CSV how to use this solution?

Posted on 2008-10-24
4
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does GeoJson relates to Leaflet and Mapbox? 4 50
Run software updates from the website 6 51
Intermittent OleDbConnection Error 20 54
.NET universe documentation poster 2 27
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:…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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