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 8134245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:56:42+00:00 2026-06-06T09:56:42+00:00

I have a DataGridView with four Columns and need to crate a multiline string

  • 0

I have a DataGridView with four Columns and need to crate a multiline string from its content, separated by comma.
This code works, but probably – there is a more elegant way:

string multiLine = "";
string singleLine;    
foreach (DataGridViewRow r in dgvSm.Rows)
{
    if (!r.IsNewRow)
    {
        singleLine = r.Cells[0].Value.ToString() + ","
        + r.Cells[1].Value.ToString() + ","
        + r.Cells[2].Value.ToString() + ","
        + r.Cells[3].Value.ToString() + Environment.NewLine;
        multiLine = multiLine + singleLine;
    }
}
  • 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-06-06T09:56:44+00:00Added an answer on June 6, 2026 at 9:56 am

    I don’t know about elegant, but:

    1. use StringBuilder for string manipulation, type string is immutable!
    2. if you need to do something in between, separate first or last cycle running (e.g. comma separation)
      So, basically something like this:
    StringBuilder multiLine = new StringBuilder();
    foreach (DataGridViewRow r in dgvSm.Rows)
    {
     if (!r.IsNewRow)
     {
      if (r.Cells.Count > 0)
      {
       multiLine.Append(r.Cells[0].Value.ToString()); //first separated
       for (int i = 1; i < r.Cells.Count; ++i)
       {
        singleLine.Append(','); //between values
        singleLine.Append(r.Cells[i].Value.ToString());
       }
       multiLine.AppendLine();
      }
     }
    }

    To illustrate speed difference between StringBuilder concatenation (just dynamic array of characters) and string (new object and copy everything each time you use operator + concatenation), have a look at mini-program:

    public static void Main()
    {
        var sw = new Stopwatch();
        sw.Start();
        StringBuilder s = new StringBuilder();
        //string s = "";
        int i;
        for (i = 0; sw.ElapsedMilliseconds < 1000; ++i)
            //s += i.ToString();
            s.Append(i.ToString());
        sw.Stop();
        Console.WriteLine("using version with type " + s.GetType().Name + " I did " +
            i + " times of string concatenation.");
    }

    For my computer it is:

    using version with type String I did 17682 times of string concatenation.
    using version with type StringBuilder I did 366367 times of string concatenation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView in a windows form. I defined 5 columns for this
I have datagridview which fills data from database, there are columns where I have
i have one datagridview.in my datagridview is having four row and three columns.after enter
I have a DataGridView with an edit option. I did my code, but this
I have a datagrid view with four columns: productid productname productprice buy (this is
I have dataGridView with many columns and rows. Now I'd like to bind content
I have a DataGridView and I need to use a custom sorter (derived from
I have DataGridView with two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn) and the Second
I have datagridview with checkbox column, but I want, that some of the columns
I have a datagridview, and it loads information from db. However, at run time,

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.