I'm trying to change the widths of columns in an ASP:Table but I can't figure out how. I've used the following code.
You'll notice in my code I'm adding two rows to my table, and then dynamically adding a row for each team. You'll also notice I'm setting the width of every cell to 400. But, when the table shows up, every cell has been auto-sized to fit the width of the largest field in that column. I tried what's mentioned in thread855-1106016 but I couldn't get that to work either. Any suggestions? Thanks.
Code:
Private Sub FillShiftSchedule()
Dim sSQL As String
Dim adoxConn As SqlConnection
Dim adoxCmd As SqlCommand
Dim adoxReader As SqlDataReader
Try
Dim newDayRow As New TableRow
Dim newDateRow As New TableRow
'*** Add cells to Rows ***
For counter As Integer = 1 To Date.DaysInMonth(cboYear.SelectedValue, cboMonth.SelectedValue)
Dim newCellDay As New TableCell
Dim newCellDate As New TableCell
newCellDay.Text = Format(Convert.ToDateTime(cboMonth.SelectedValue & "/" & counter & "/" & cboYear.SelectedValue), "ddd") & "."
newCellDate.Text = Format(Convert.ToDateTime(cboMonth.SelectedValue & "/" & counter & "/" & cboYear.SelectedValue), "M/d/yyyy")
'*** Format Cells ***
newCellDay.BorderWidth = Unit.Pixel(1)
newCellDay.BorderColor = Black
newCellDay.Width = Unit.Pixel(400)
newCellDate.BorderWidth = Unit.Pixel(1)
newCellDate.BorderColor = Black
newCellDate.Width = Unit.Pixel(400)
newDayRow.Cells.Add(newCellDay)
newDateRow.Cells.Add(newCellDate)
Next
'*** Format Day Row ***
newDayRow.Font.Bold = True
newDayRow.Font.Name = "Arial"
newDayRow.HorizontalAlign = HorizontalAlign.Center
newDayRow.BackColor = Color.FromArgb(51, 255, 255)
'*** Format Date Row ***
newDateRow.Font.Bold = True
newDateRow.Font.Name = "Arial"
newDateRow.HorizontalAlign = HorizontalAlign.Center
'*** Add Rows to Table ***
tblSchedule.Rows.Add(newDayRow)
tblSchedule.Rows.Add(newDateRow)
adoxConn = New SqlConnection
adoxConn.ConnectionString = Session("ConnectionString")
adoxConn.Open()
sSQL = " SELECT sd.ShiftDate,t.Team,st.ShiftTime "
sSQL &= "FROM Team t "
sSQL &= "LEFT JOIN ShiftDate sd ON sd.TeamID = t.TeamID "
sSQL &= "LEFT JOIN ShiftTime st ON sd.ShiftTimeID = st.ShiftTimeID "
sSQL &= "WHERE Month(sd.ShiftDate) = '" & cboMonth.SelectedValue & "' "
sSQL &= "AND Year(sd.ShiftDate) = '" & cboYear.SelectedValue & "' "
sSQL &= "ORDER BY t.team, sd.ShiftDate"
adoxCmd = New SqlCommand(sSQL, adoxConn)
adoxReader = adoxCmd.ExecuteReader()
Dim newRow As TableRow
Dim newCell As TableCell
Dim currentTeam As String = ""
Dim currentDay As Integer = 1
While adoxReader.Read()
If adoxReader("Team") <> currentTeam Then
If currentTeam <> "" Then
For i As Integer = currentDay To Date.DaysInMonth(cboYear.SelectedValue, cboMonth.SelectedValue)
'*** Add Cells to Rows ***
newCell = New TableCell
newCell.BorderWidth = Unit.Pixel(1)
newCell.BorderColor = Black
newRow.Cells.Add(newCell)
currentDay += 1
Next
tblSchedule.Rows.Add(newRow)
Else
lblA.Text = adoxReader("Team")
End If
newRow = New TableRow
newRow.Font.Name = "MS Sans Serif"
newRow.HorizontalAlign = HorizontalAlign.Center
If tblSchedule.Rows.Count Mod 2 = 0 Then
newRow.BackColor = LightGray
Else
newRow.BackColor = White
End If
currentTeam = adoxReader("Team")
currentDay = 1
End If
'*** Create and format cells ***
newCell = New TableCell
newCell.BorderWidth = Unit.Pixel(1)
newCell.BorderColor = Black
newCell.Width = Unit.Pixel(400)
newCell.Text = adoxReader("ShiftTime")
newRow.Cells.Add(newCell)
currentDay += 1
End While
For i As Integer = currentDay To Date.DaysInMonth(cboYear.SelectedValue, cboMonth.SelectedValue)
newCell = New TableCell
newCell.BorderWidth = Unit.Pixel(1)
newCell.BorderColor = Black
newRow.Cells.Add(newCell)
currentDay += 1
Next
tblSchedule.Rows.Add(newRow)
Catch ex As Exception
Finally
If Not adoxReader Is Nothing Then adoxReader.Close()
adoxConn.Close()
End Try
End Sub