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
Solved

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

Posted on 2014-03-30
3
787 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:murbro
3 Comments
 
LVL 35

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 500 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:murbro
ID: 39965803
Thanks very much Alan
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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