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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:37:53+00:00 2026-06-06T00:37:53+00:00

How can I convert a single datarow (of a datatable) into a CSV like

  • 0

How can I convert a single datarow (of a datatable) into a CSV like string with only few C# commands.
In other words into a string like “value_1;value_2;…;value_n”

  • 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-06T00:37:54+00:00Added an answer on June 6, 2026 at 12:37 am

    Quick and dirty for a single DataRow:

    System.Data.DataRow row = ...;
    
    string csvLine = String.Join(
        CultureInfo.CurrentCulture.TextInfo.ListSeparator,
        row.ItemArray);
    

    If you do not care about the culture specific separator you may do this to convert a full DataTable:

    public static string ToCsv(System.Data.DataTable table)
    {
        StringBuilder csv = new StringBuilder();
    
        foreach (DataRow row in table.Rows)
            csv.AppendLine(String.Join(";", row.ItemArray));
    
        return csv.ToString();
    }
    

    Here a more complex example if you need to handle a little bit of formatting for the values (in case they’re not just numbers).

    public static string ToCsv(DataTable table)
    {
        StringBuilder csv = new StringBuilder();
        
        foreach (DataRow row in table.Rows)
        {
            for (int i = 0; i < row.ItemArray.Length; ++i)
            {
                if (i > 0)
                    csv.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
        
                csv.Append(FormatValue(row.ItemArray[i]));
            }
        
            csv.AppendLine();
        }
        
        return csv.ToString();
    }
    

    Or, if you prefer LINQ (and assuming table is not empty):

    public static string ToCsv(DataTable table, string separator = null)
    {
        if (separator == null)
            separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
    
        return table.Rows
            .Cast<DataRow>()
            .Select(r => String.Join(separator, r.ItemArray.Select(c => FormatValue(c)))
            .Aggregate(new StringBuilder(), (result, line) => result.AppendLine(line))
            .ToString();
    }
    

    Using this private function to format a value. It’s a very naive implementation, for non primitive types you should use TypeConverter (if any, see this nice library: Universal Type Converter) and quote the text only if needed (2.6):

    private static string FormatValue(object value)
    {
        if (Object.ReferenceEquals(value, null))
            return "";
    
        Type valueType = value.GetType();
    
        if (valueType.IsPrimitive || valueType == typeof(DateTime))
            return value.ToString();
    
        return String.Format("\"{0}\"",
            value.ToString().Replace("\"", "\"\"");
    }
    

    Notes
    Even if there is a RFC for CSV many applications does not follow its rules and they handle special cases in their very own way (Microsoft Excel, for example, uses the locale list separator instead of comma and it doesn’t handle newlines in strings as required by the standard).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like a function to convert a single dimension array int[480000] into a
How can I convert 1.bmp 2.bmp ... n.bmp (each 24 bpp) into a single
I wish to convert a single string with multiple delimiters into a key=>value hash
How can I convert single channel IplImage (grayscale), depth=8, into a Bitmap? The following
Is there anyway i can convert the below 3 sql queries into a single
I have a matrix (32X48). How can I convert the matrix into a single
How can I convert any record type to a single String and back? Perhaps
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
So I know I can convert a string to a hashcode simply by doing
I have some code that can convert a single color in a template image

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.