align columns headers with cells values in a text file that exported from a datagridview in c#
-
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();
-
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();
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
-
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
Thanks for your answer.