iTextSharp align and alternate row color

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

iTextSharp align and alternate row color

Oscar
I am reading data from a database, and populating a table using iTextSharp. This is the way I'm reading the data:

var db = Database.Open("NewDatabase");
var sql =
    @"SELECT FullName,
                [2017-01-02], [2017-01-03], [2017-01-04],
                ([2017-01-02] + [2017-01-03] + [2017-01-04]) as 'DatesSum',
                [Week1]
    FROM        [NewTable]";

var data = db.Query(sql);
var columns = data.First().Columns;                                                                          
var doc = new Document();                                                                                  
PdfWriter.GetInstance(doc, Response.OutputStream);                                                          
doc.Open();
PdfPTable table = new PdfPTable(new float[] {30f, 10f, 10f, 10f, 10f, 10f});

foreach(var row in data)                                                        
{
    foreach(var column in columns)
    {
        table.AddCell(new Phrase(row[column] != null ? row[column].ToString() : string.Empty));
    }
}
       
doc.Add(table);
doc.Close();

As you can see, I'm using 'foreach' to loop through the data and populate the table. That is the only way I know of populating the table (I'm fairly new to iTextSharp).

I have been trying to align the data to the middle of the cells. Also, I want to alternate the color of the rows (every other row). I have been trying things like this, inside the loop, with no results:

row[column].HorizontalAlignment = 1;