Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Width of cells in an ASP:Table

Status
Not open for further replies.

jonbatts

Programmer
Apr 12, 2005
114
US
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.
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
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.
 
Try specifying a New Unit when assigning the values.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Code:
Catch ex As Exception    
Finally
    If Not adoxReader Is Nothing Then adoxReader.Close()
    adoxConn.Close()
End Try

wont that cause probs without an End If? Dont u get errors when running this?
Closing it twice?

The Finally section occurs at all times after exception handling, in which you do nothing.

you are trying, failing somewhere, and not checking for or displaying anything.

just a thought//

Dim success As Boolean = True
Try
'Bla
Catch ex As Exception
success = false
message.text = ex.Message
Finally
bla.close() 'close your connection error or not
End Try

If success = true then
'continue doing stuff
End If
 
ca8msm, instead of [tt]newCellDay.Width = Unit.Pixel(400)[/tt], I tried [tt]newCell.Width = New Unit(400, UnitType.Pixel)[/tt], but to no avail. Any other ideas? Thanks.
 
adamroof, a VB.NET "If, Then" statement can be stated
Code:
If X Then
    Y()
End If
or
Code:
If X Then Y()
The latter being popular if you don't need more than one line of code run if X is True. To your other suggestions I'm just using a generic catch-all Try statement. I definitely should have some error message in my Catch, just haven't gotten around to it yet. I should also have my [tt]"adoxconn.close"[/tt] in an If Then statement. If my Try errors out before it gets to the line [tt]adoxConn = New SQLConnection[/tt], then my Finally block will throw an uncaught error.
In reference to using your Boolean "success" variable, it's not necessary if you place an [tt]Exit Sub[/tt] at the end of your Catch block.
Thanks for the response and suggestions.
 
Alright, I figured out the problem. When I added [tt]style="TABLE-LAYOUT: fixed"[/tt] to the ASP:Table tag in the HTML, the widths did exactly what they were supposed to. Apparently the default table layout was compressing the widths. Thanks for your time guys.
 
Another problem: I can statically place [tt]style="TABLE-LAYOUT: fixed"[/tt] in the HTML, but what if I dynamically create a table in the code? How do I set its [tt]TABLE-LAYOUT[/tt]? Thanks.
 
It will cause error to exit out of a try catch with an exit sub, give it a try catch!

It was just my experience and the way i did things, which is good and bad about vb, so many ways to do one thing. Appreciated the response, and didnt realize you were closing reader then connection, thought it was your conn closing twice.

as for the layout...

tblSchedule.Attributes.Add("style", "TABLE-LAYOUT: fixed")

just after your table declaration
 
Thanks adam! I was just about to post the solution myself, but my solution's different than yours.

[tt]tblSchedule.Style.Item("TABLE-LAYOUT") = "fixed"[/tt]
...good and bad about vb, so many ways to do one thing.
:) Thanks a lot for your time.
 
adamroof said:
It will cause error to exit out of a try catch with an exit sub, give it a try catch!
I tried
Code:
Try
    X()
Catch ex As Exception
    lblFeedback.Text = "X failed.  "
    Exit Sub
Finally
    Y()
    lblFeedback.Text &= "Y Completed Successfully.  "
End Try
lblFeedback.Text = "X Completed Successfully.  "
If X threw an error, lblFeedback.text equaled "[tt]X failed. Y Completed Successfully. [/tt]", but if X Completed successfully lblFeedback.text equaled "[tt]Y Completed Successfully. X Completed Successfully. [/tt]
 
i
Tried
you caught me
Finally
i accept defeat
End Try

at some point in past, maybe different ui, it warned me of exiting sub, but i tried again, same results, no errors...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top