Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

datagridview to CSV how to use this solution?

Posted on 2008-10-24
4
Medium Priority
?
752 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 800 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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