none
How to wrap text value of datatable row's in C#? RRS feed

  • Question

  • Hi all,
    I am exporting my DataTable values to .txt file using code and its working fine.
    i want to wrap text data of Rows value so that it can will look good.
    means i want to display it in proper manner.

    below is my ouput

    ID | Name | Address
    1 | VenkVenkVenkVenkVenkVenkVe | chennai
    2 | Hanu | London
    3 | john | Swiss

    i want to wrap data of name Column

    i want to output like this :
     
     
     ID       |    Name                   |    Address
    1          | VenkVenkVenk
               VenkVenkVenkVe    | chennai   

    2          | Hanu                       | London
    3          | john                        | Swiss
     
     Below is my code
     
    private void CreateDT()
    {
    DataTable dt = new DataTable();
    //Adding data To DataTable
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    dt.Columns.Add("Address");
    dt.Rows.Add(1, "VenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenkVenk", "chennai");
    dt.Rows.Add(2, "Hanu", "London");
    dt.Rows.Add(3, "john", "Swiss");
    // DataGridView grid = new DataGridView();
    int i = 0;
    StreamWriter swExtLogFile = new StreamWriter("D:/DTlog2.txt", true);
    //StreamWriter sw = null;
    // sw = new StreamWriter(submittedFilePath, false);
    for (i = 0; i < dt.Columns.Count - 1; i++)
    {
    swExtLogFile.Write(dt.Columns[i].ColumnName + "\t" +"\t" + " | ");
    }
    swExtLogFile.Write(dt.Columns[i].ColumnName);
    swExtLogFile.WriteLine();
    foreach (DataRow row in dt.Rows)
    {
    object[] array = row.ItemArray;
    for (i = 0; i < array.Length - 1; i++)
    {
    swExtLogFile.Write(array[i].ToString() + "\t" + "\t" + " | ");
    }
    swExtLogFile.Write(array[i].ToString());
    swExtLogFile.WriteLine();
    }
    swExtLogFile.Close();
    }

     ID       |    Name                   |    Address
    1          | VenkVenkVenk
               VenkVenkVenkVe    | chennai   

    2          | Hanu                       | London
    3          | john                        | Swiss
     
     ID       |    Name                   |    Address
    1          | VenkVenkVenk
               VenkVenkVenkVe    | chennai   

    2          | Hanu                       | London
    3          | john                        | Swiss
     

    sumitk

    Thursday, September 28, 2017 9:07 AM

All replies

  • Hello,

    Please edit your post, reformat the code using the code button.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, September 28, 2017 12:41 PM
    Moderator
  • You don't want to wrap the text in a text file. You're using pipes to identify columns. If you put a single row on multiple lines then you won't be able to import that file into anything that understands the delimiters (i.e. Excel or any CSV reader) because each row is assumed to be a new record.

    Why do you want to generate a text file that has formatted text but still use a delimiter? If this is for a user to read (the only reason to pretty print it) then don't use vertical bars. Ideally consider generating an HTML file instead which can be easily formatted and viewed in a browser. If this is for consumption by other systems then don't pretty print it. There is no benefit and it will actually complicate things.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, September 28, 2017 2:02 PM
    Moderator