Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. align columns headers with cells values in a text file that exported from a datagridview in c#

align columns headers with cells values in a text file that exported from a datagridview in c#

Scheduled Pinned Locked Moved C#
csharpc++csscomworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13293914
    wrote on last edited by
    #1

    I export my data from a DGV and align them with a space delimiter and it is working good for me. i also export the headers before the cells values. the thing that i want to do but i can not is aligning and adjusting the cells with headers to see them as a table that its members are separated by a delimiter (here space). my means is i want to see cells values right below the headers (if they have smaller or equal lengths) and headers text right on the top of the cells values if header length is less than cells values lengths), in the other word, Keeping cells values and their related headers together.(i am using Microsoft.Office.Interop.Word.) thanks in advance. What i have been tried:

    int RowsCount = DataTable.Rows.Count;
    int ColumnsCount = DataTable.Columns.Count;
    var newline = System.Environment.NewLine;
    var delimiter = " ";
    var copied= new StringBuilder();
    List clmnLocation = new List() {};

    string header = "";
    for (int c = 0; c < DataTable.Columns.Count; c++)
    {
    header = header + Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ", "") + delimiter;

    //Count header length to get the padright() value and put in a list
    clmnLocation.Add(header.Length);
    }

    // Export titles:
    for (int c = 0; c < DataTable.Columns.Count; c++) copied.Append(Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ","") + delimiter);

    //Append A Line After Header.
    clipboard_string.Append(newline);

                    //Cells
                    for (int i = 0; i  0)
    

    //here i am PadRight(int beforeheaders.Length)
    copied.Append(DataTable.Rows[i][j].ToString().PadRight(clmnLocation[j]) + delimiter);
    else
    copied.Append(DataTable.Rows[i][j].ToString() + delimiter);
    }
    copied.Append(newline);
    }

                    WordDoc.Content.Text = copied.ToString();
    
    L 1 Reply Last reply
    0
    • U User 13293914

      I export my data from a DGV and align them with a space delimiter and it is working good for me. i also export the headers before the cells values. the thing that i want to do but i can not is aligning and adjusting the cells with headers to see them as a table that its members are separated by a delimiter (here space). my means is i want to see cells values right below the headers (if they have smaller or equal lengths) and headers text right on the top of the cells values if header length is less than cells values lengths), in the other word, Keeping cells values and their related headers together.(i am using Microsoft.Office.Interop.Word.) thanks in advance. What i have been tried:

      int RowsCount = DataTable.Rows.Count;
      int ColumnsCount = DataTable.Columns.Count;
      var newline = System.Environment.NewLine;
      var delimiter = " ";
      var copied= new StringBuilder();
      List clmnLocation = new List() {};

      string header = "";
      for (int c = 0; c < DataTable.Columns.Count; c++)
      {
      header = header + Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ", "") + delimiter;

      //Count header length to get the padright() value and put in a list
      clmnLocation.Add(header.Length);
      }

      // Export titles:
      for (int c = 0; c < DataTable.Columns.Count; c++) copied.Append(Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ","") + delimiter);

      //Append A Line After Header.
      clipboard_string.Append(newline);

                      //Cells
                      for (int i = 0; i  0)
      

      //here i am PadRight(int beforeheaders.Length)
      copied.Append(DataTable.Rows[i][j].ToString().PadRight(clmnLocation[j]) + delimiter);
      else
      copied.Append(DataTable.Rows[i][j].ToString() + delimiter);
      }
      copied.Append(newline);
      }

                      WordDoc.Content.Text = copied.ToString();
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you want "headers" aligned in a (mono-spaced) text file, you will first need to determine the "maximum length / width" of each value in each column (plus a space for a column separator, if not "tabbing"). Besides aligning the headers, you will also need to align the columns. Easier to just open in Excel as a CSV if you want to "look" at it.

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      U 1 Reply Last reply
      0
      • L Lost User

        If you want "headers" aligned in a (mono-spaced) text file, you will first need to determine the "maximum length / width" of each value in each column (plus a space for a column separator, if not "tabbing"). Besides aligning the headers, you will also need to align the columns. Easier to just open in Excel as a CSV if you want to "look" at it.

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        U Offline
        U Offline
        User 13293914
        wrote on last edited by
        #3

        Thanks for your answer.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups