Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:28:47+00:00 2026-05-25T01:28:47+00:00

How do I print specified rows out to a file from a DataGridView? Also

  • 0

How do I print specified rows out to a file from a DataGridView?
Also how can I print out certain columns?

This is what I have been trying to work with.. but it is not working..:

    private void saveButton_Click(object sender, EventArgs e)
    {
            saveFile1.DefaultExt = "*.txt";
            saveFile1.Filter = ".txt Files|*.txt|All Files (*.*)|*.*";
            saveFile1.RestoreDirectory = true;

            if (saveFile1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveFile1.FileName);

                List<string> theList = new List<string>();

                foreach (var line in theFinalDGV.Rows)
                {
                    if (line.ToString().Contains("FUJI"))
                        richTextBox1.AppendText(line + "\n");
                }
            }
    }

Can anyone help me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:28:48+00:00Added an answer on May 25, 2026 at 1:28 am

    DataGridViewRow.ToString() only gives you the typename back, not the row content. You can use this extender to get the rowcontent (‘ColumnName’):

    public static class Extender {
        public static string RowToString(this DataGridViewRow dgvr) {
            string output = "";
    
            DataGridView dgv = dgvr.DataGridView;
    
            foreach (DataGridViewCell cell in dgvr.Cells) {
    
                DataGridViewColumn col = cell.OwningColumn;
                output += col.HeaderText + ":" + cell.Value.ToString() + ((dgv.Columns.IndexOf(col) < dgv.Columns.Count - 1) ? ", " : "");
            }
    
            return output;
        }
    }
    

    if you only want the content of the row without the coulmn-headername use this (space separated):

    public static class Extender {
        public static string RowToString(this DataGridViewRow dgvr) {
            string output = "";
    
            foreach (DataGridViewCell cell in dgvr.Cells) {
                output += cell.Value.ToString() + " ";
            }
    
            return output.TrimEnd();
        }
    }
    

    your code will look like this:

    class YourClass
    {
        private void saveButton_Click(object sender, EventArgs e)
        {
                saveFile1.DefaultExt = "*.txt";
                saveFile1.Filter = ".txt Files|*.txt|All Files (*.*)|*.*";
                saveFile1.RestoreDirectory = true;
    
                if (saveFile1.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(saveFile1.FileName);
    
                    List<string> theList = new List<string>();
    
                    foreach (var line in theFinalDGV.Rows)
                    {
                        string linecontent = line.RowToString();
                        if (linecontent.Contains("FUJI"))
                            richTextBox1.AppendText(linecontent + "\n");
                    }
                }
        }
    }
    
    public static class Extender {
        public static string RowToString(this DataGridViewRow dgvr) {
            string output = "";
    
            DataGridView dgv = dgvr.DataGridView;
    
            foreach (DataGridViewCell cell in dgvr.Cells) {
    
                DataGridViewColumn col = cell.OwningColumn;
                output += col.HeaderText + ":" + cell.Value.ToString() + ((dgv.Columns.IndexOf(col) < dgv.Columns.Count - 1) ? ", " : "");
            }
    
            return output;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I print the columns specified in fields_i_want instead of hardcoding the column
I have been literally pulling my hair out with this one and its beginning
I have a map made up of rows and columns of hexagons This isn't
I'm trying to print characters in the console at specified coordinates. Up to now
I have a text file, and I need to print it to a specific
Lets say i have a simple array of x rows and y columns with
I've been working on this simple code that puts a CSV file into a
I need to print out csv file into html or put a numeric data
Well I have been stumped as to the best way to do this, I
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.