Reputation: 11
I am trying to format my datatable so that all the columns line up correctly. The issue is that since each cell contains different lengths in characters, each cell has more or less than what I would like it to have. Is there an easy way to format a dataTable cell? The following code is what I have to create the table:
public static void ErrorLog(OdbcConnection connection, string filepath)
{
int ID = 0;
DateTime now = DateTime.Now;
XDocument theFile = XDocument.Load("C:\\Users\\bob\\Documents\\UpdateW5.xml");
DataTable theTable = new DataTable("Error Log");
//Set the report filename
string filename = @"ErrorLog.html";
//Check to see if directory exists, if not create it.
if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
//Open file for output
TextWriter webPage = new StreamWriter(filepath + filename, false);
//Page header
webPage.WriteLine("<html>");
webPage.WriteLine("<head>");
webPage.WriteLine("<title>ErrorLog</title");
webPage.WriteLine("</head>");
//Page body
webPage.WriteLine("<body>");
//Start an output table
webPage.WriteLine("<h1>ErrorLog</h1>");
webPage.WriteLine("<table border=1>");
webPage.WriteLine("<tr>"); //header row of table
webPage.WriteLine("<th>ERROR ID</th>");
webPage.WriteLine("<th>ITEM ID</th>");
webPage.WriteLine("<th>DATE/TIME</th>");
webPage.WriteLine("<th>DESCRIPTION</th>");
foreach (XElement el in theFile.Root.Elements())
{
//If the node item is called ADD run the following code
if (el.Name == "ADD")
{
//Check if each attribute has a value
if (el.Attribute("invent_id") != null && el.Attribute("item_id") != null && el.Attribute("itemsize") != null && el.Attribute("color") != null && el.Attribute("curr_price") != null && el.Attribute("qoh") != null)
{
Item.Item_ID = Convert.ToInt32(el.Attribute("item_id").Value);
Item.Invent_id = Convert.ToInt32(el.Attribute("invent_id").Value);
Item.Itemsize = el.Attribute("itemsize").Value;
Item.Color = el.Attribute("color").Value;
Item.Curr_price = Convert.ToDecimal(el.Attribute("curr_price").Value);
Item.Qoh = Convert.ToInt32(el.Attribute("qoh").Value);
if (Item.Qoh < 1)
{
webPage.WriteLine("<table border=1>");
webPage.WriteLine("<col align = justify>");
webPage.WriteLine("<tr>"); //header row of table
webPage.WriteLine("<td>" + ID++ + "</td>");
webPage.WriteLine("<td>" + now + "</td>");
webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
webPage.WriteLine("<td> not added: The quantity must be positive</td>");
}
//If the quantity is above 1, then run the queries
else
{
//Check to make sure the item is not already in the database
connection.Open();
OdbcCommand Command1 = new OdbcCommand("SELECT * FROM inventory WHERE invent_id = '" + Item.Invent_id + "'", connection);
{
//If it is in the database, error out
OdbcDataReader reader = Command1.ExecuteReader();
if (reader.HasRows)
{
webPage.WriteLine("<table border=1>");
webPage.WriteLine("<tr>"); //header row of table
webPage.WriteLine("<td>" + ID++ + "</td>");
webPage.WriteLine("<td>" + now + "</td>");
webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
webPage.WriteLine("<td> already in database.</td>");
}
//If it is not in the database, add the item into the table
else
{
Console.WriteLine("Item {0} ", el.Attribute("item_id").Value + " was successfully added.");
Console.ReadLine();
OdbcCommand Command2 = new OdbcCommand("LOAD XML LOCAL INFILE 'C:/Users/abechtold/Documents/Update.xml' INTO TABLE item ROWS IDENTIFIED BY '<ADD>'", connection);
connection.Open();
OdbcDataReader reader2 = Command1.ExecuteReader();
connection.Close();
}
reader.Close();
reader.Dispose();
}
connection.Close();
}
}
//Check if the invent id is empty, if so, do not run any of the code for ADD
else if (el.Attribute("invent_id") == null)
{
Item.Item_ID = Convert.ToInt32(el.Attribute("item_id").Value);
webPage.WriteLine("<table border=1>");
webPage.WriteLine("<tr>"); //header row of table
webPage.WriteLine("<td>" + ID++ + "</td>");
webPage.WriteLine("<td>" + now + "</td>");
webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
webPage.WriteLine("<td> not added: An input value was an improper datatype</td>");
}
}
}
webPage.WriteLine("</table>"); //end table
webPage.WriteLine("</body>");
webPage.WriteLine("</html>");
webPage.Flush();//Make sure all characters are in the file and not buffered.
webPage.Close();//Closes the file and officially writes it to disk.
}
Upvotes: 0
Views: 38
Reputation: 11
Crowcoder's comment helped me answer my question. Since I had
webPage.WriteLine("<table border=1>");
written in every if, else statement, it was creating its own row of a table. By removing this line from every statement, and keeping just 1 in the "Start an output table" spot. This made it so my columns line up and provide correct spacing.
Upvotes: 1