Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.net Dynamically adding a table with a solid row color

Posted on 2014-03-30
3
Medium Priority
?
847 Views
Last Modified: 2014-03-30
Hi

I am dynamically adding the table below to an ASP.net web page using the code
further down. I want the rows to have a bottom row with a solid row color and border line that runs right through. I generated the second image in Excel, and this shows
how I want the table to look. How do I adjust the VB.net code to achieve this. Thanks


1
2

  Function oTableReport() As Table
        Try

            Dim rowCnt As Integer
            ' Current row count
            Dim rowCtr As Integer
            ' Total number of cells (columns).
            Dim cellCtr As Integer
            ' Current cell counter.
            Dim cellCnt As Integer
            Dim Table1 As New Table
            rowCnt = 2
            cellCnt = 3
            Table1.BorderWidth = 1
            Table1.BorderColor = Drawing.Color.Black
            Table1.BorderStyle = BorderStyle.Solid
            Table1.GridLines = GridLines.None

            For rowCtr = 1 To rowCnt
                Dim tRow As New TableRow()
                tRow.BackColor = Drawing.Color.Aqua
                For cellCtr = 1 To cellCnt
                    Dim tCell As New TableCell()
                    tCell.Text = "Row " & rowCtr & ", Cell " & cellCtr
                    'tCell.BackColor = Drawing.Color.White
                    tCell.ForeColor = Drawing.Color.Blue
                    ' Add new TableCell object to row.
                    tRow.Cells.Add(tCell)
                Next
                ' Add new row to table.
                Table1.Rows.Add(tRow)
            Next
            Return Table1
        Catch ex As Exception
            MsgBox(ex.Message & " rew33")
        End Try
    End Function
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 39965651
Very hard to do it in code behind alone.
Please remove the following code from your sample above:
            Table1.BorderWidth = 1
            Table1.BorderColor = Drawing.Color.Black
            Table1.BorderStyle = BorderStyle.Solid
            Table1.GridLines = GridLines.None

Open in new window

and add the following  CSS rules to your mark-up head section as follows:
<head>
<style>
table,th,td
{
border-bottom:1px solid black;
border-spacing: 0px;
}
</style>
</head>

Open in new window

where:
CSS border-spacing removes cell spacing when the value is 0px.
CSS border-bottom creates the bottom border on every header and cell of your table.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 39965766
Hi murbro,
Try something like this:
<%@ Page Language="VB" %>

<!DOCTYPE html>

<script runat="server">
  Function oTableReport() As Table
    oTableReport = Nothing

    Try

      Dim rowCnt As Integer
      ' Current row count
      Dim rowCtr As Integer
      ' Total number of cells (columns).
      Dim cellCtr As Integer
      ' Current cell counter.
      Dim cellCnt As Integer
      Dim Table1 As New Table

      rowCnt = 2
      cellCnt = 3

      Table1.GridLines = GridLines.None

      For rowCtr = 1 To rowCnt
        Dim tRow As New TableRow()

        If rowCtr < rowCnt Then

          For cellCtr = 1 To cellCnt
            Dim tCell As New TableCell()
            tCell.Text = "Row " & rowCtr & ", Cell " & cellCtr

            ' Add new TableCell object to row.
            tRow.Cells.Add(tCell)
          Next

        Else ' it's the last row

          For cellCtr = 1 To cellCnt
            Dim tCell As New TableCell()

            tCell.Text = "Row " & rowCtr & ", Cell " & cellCtr
            tCell.CssClass = "lastrow"  ' <<<== apply lastrow style to cells in the last row

            ' Add new TableCell object to row.
            tRow.Cells.Add(tCell)
          Next

        End If

        ' Add new row to table.
        Table1.Rows.Add(tRow)
      Next
      Return Table1
    Catch ex As Exception
      ' MsgBox(ex.Message & " rew33")
      System.Diagnostics.Debug.WriteLine(ex.Message)
    End Try

  End Function

  Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Me.Controls.Add(oTableReport())
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <style type="text/css">
    table {
      border: none;
      border-collapse:collapse;
      border-spacing:0;

    }
      table tr {
        border: none;
      }
      table td {
        border: none;
      }

    table td.lastrow {
      border-left:none;
      border-right:none;
      border-top:1px solid black;
      border-bottom:3px double black; /*need about 3px to render double line border*/
      background-color:lightblue;
    }
  </style>

    <title>Q_28400693</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
      <asp:button ID="Button1" runat="server" text="Create Table" OnClick="Button1_Click" />   
    </div>
  </form>
</body>
</html>

Open in new window

Looks like:
Q_28400693
Respectfully yours,
Alan
0
 

Author Closing Comment

by:Murray Brown
ID: 39965803
Thanks very much Alan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question