We help IT Professionals succeed at work.

how to programatically design a table in c#

Medium Priority
986 Views
Last Modified: 2012-05-06
I am creating dynamic label controls in asp.net and adding them in asp.net table element.  How could we programmatically set cell spacing, background color of the row and insert line breaks(<br />) between the rows. I have tried all of these but for some reasons they dont seem to work. I am attaching  my code. I will appreciate any help. Thanks,
TableRow tr = new TableRow();
        TableCell SubjectLabelCell = new TableCell();
 
        Label _labelHeading;
 
         _labelHeading = new Label();
 
            _labelHeading.Text = "Subject";  
            _labelHeading.CssClass = "GenericLabel";
            _labelHeading.ForeColor = System.Drawing.ColorTranslator.FromHtml("#003333");
             SubjectLabelCell.Controls.Add(_labelHeading);
            SubjectLabelCell.Attributes.Add("backcolor", "Gray");

Open in new window

Comment
Watch Question

Try this
        HtmlTable tbl = new HtmlTable();
        tbl.CellPadding = 2;
        tbl.CellPadding = 2;
        TableRow tblRow = new TableRow();
        tblRow.BackColor = System.Drawing.Color.Gray;
        TableCell tblCell = new TableCell();
        Label lblHeading = new Label();
        lblHeading.Text = "Subject";  
        lblHeading.CssClass = "GenericLabel";
        lblHeading.ForeColor = System.Drawing.ColorTranslator.FromHtml("#003333");
        tblCell.Controls.Add(lblHeading);
        tblRow.Cells.Add(tblCell);
        tbl.Rows.Add(tblRow);
        Page.Controls.Add(tbl);

Open in new window

Why would you actually want to add <br/> between rows?? Adding <br/> between rows is not a valid html. If you mean you want to add <br/> inside the table cell then you can do it in the label that you add or directly to the table cell like this tblCell.Text="<br/>";
You need to ouse the appropriate properties of the relevant class like this:


Table myTable = new Table();
myTable.CellPadding = 10; // Sets the padding
 
TableRow tr = new TableRow();
tr.BackColor = "LightGreen"; // Sets the back color of the row
tr.ForeColor = "Blue"; // Sets the text color of the row
 
TableCell SubjectLabelCell = new TableCell();
SubjectLabelCell.BorderWidth = 2; // Sets border width
 
// Add controls to cell
 
myTable.Rows.Add(tr);
tr.Cells.Add(SubjectLabelCell);
... etc

Open in new window

Sorry try this
        Table tbl = new Table();
        tbl.CellPadding = 2;
        tbl.CellPadding = 2;
        TableRow tblRow = new TableRow();
        tblRow.BackColor = System.Drawing.Color.Gray;
        TableCell tblCell = new TableCell();
        Label lblHeading = new Label();
        lblHeading.Text = "Subject";  
        lblHeading.CssClass = "GenericLabel";
        lblHeading.ForeColor = System.Drawing.ColorTranslator.FromHtml("#003333");
        tblCell.Controls.Add(lblHeading);
        tblRow.Cells.Add(tblCell);
        tbl.Rows.Add(tblRow);
        Page.Controls.Add(tbl);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank for the comments. I could not set the cell spacing. I have added a table element at design time. Then at run time i add rows and colums to it. I have tried setting the cell spacing at design time (<asp:Table ID="tblQuestionAnswers" runat="server" CellPadding="5" CellSpacing="5" Width="100%">
            </asp:Table>) but it did nt work n then i tried at run time again(tblQuestionAnswers.CellSpacing = Convert.ToInt16("10");) it did nt work. Any ideas,
Try looking at the resulting HTML (after running the page->View Source) , this might give you a clue...
Try adding a normal html table and see what is the outcome
<table cellpadding="5" cellspacing="5">
<tr>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
</tr>
<tr>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
</table>

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.