Free Support Forum - aspose.com

Bold Table Column Captions

How do I bold the table header column captions? This is what I have and it builds the tables perfectly but I can’t bold the column caption (the first row).

Public Sub BuildTable(ByVal id_Sales As String, sproc As String, ByVal doc As Document, ByVal builder As DocumentBuilder)

Dim dt As DataTable = New DataTable
Try
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(“cnn_BC”).ConnectionString)
Dim myCommand As New SqlCommand(sproc, myConnection)
myCommand.CommandType = CommandType.StoredProcedure

myCommand.Parameters.Add(New SqlParameter("@id_sales", SqlDbType.Int))
myCommand.Parameters("@id_sales").Value = CInt(id_Sales)

myConnection.Open()
myCommand.ExecuteNonQuery()
Dim dr As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dt.Load(dr)
myConnection.Close()
End Using
Catch myException As Exception
End Try


Dim table = builder.StartTable()
Dim row As New Row(doc)
row.RowFormat.AllowBreakAcrossPages = True
row.RowFormat.HeadingFormat = True
table.AppendChild(row)

’ We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.AutoFitToContents)

For Each myColumn As DataColumn In dt.Columns

’ Create a cell and add it to the row
Dim cell As New Cell(doc)
cell.CellFormat.Shading.ForegroundPatternColor = Color.Transparent
cell.CellFormat.Width = 80

’ Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(New Paragraph(doc))
cell.FirstParagraph.AppendChild(New Run(doc, myColumn.ColumnName))

’ Add the cell to the row.
row.AppendChild(cell)

Next myColumn

row = New Row(doc)
row.RowFormat.AllowBreakAcrossPages = True
table.AppendChild(row)

'Add the data rows.
For Each myRow As DataRow In dt.Rows

For Each myColumn As DataColumn In dt.Columns

’ Create a cell and add it to the row
Dim cell As New Cell(doc)
cell.CellFormat.Shading.BackgroundPatternColor = Color.Transparent
cell.CellFormat.Width = 80

’ Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(New Paragraph(doc))
cell.FirstParagraph.AppendChild(New Run(doc, myRow(myColumn.ColumnName).ToString()))

’ Add the cell to the row.
row.AppendChild(cell)

Next myColumn

row = New Row(doc)
row.RowFormat.AllowBreakAcrossPages = True
table.AppendChild(row)

Next myRow

'move into center of page
'table.Alignment = TableAlignment.Center

End Sub

Hi Rob,

Thanks for your inquiry. Please use the following code snippet to bold the text of first row.

Dim doc As New Document("table.docx")

For Each table As Table In doc.GetChildNodes(NodeType.Table, True)

Dim row As Row

row = table.FirstRow

For Each cell As Cell In row.Cells

For Each run As Run In cell.GetChildNodes(NodeType.Run, True)

run.Font.Bold = True

Next

Next

Next

doc.Save("AsposeOut.docx")

Please let us know if you have any more queries.

Thanks! Works like a charm!

Hi Rob,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.