Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

datagridview to CSV how to use this solution?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

885 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