[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to programatically design a table in c#

Posted on 2009-02-12
7
Medium Priority
?
975 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

0
Comment
Question by:mmalik15
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23620629
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

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23620649
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/>";
0
 
LVL 8

Expert Comment

by:SunnyDark
ID: 23620653
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

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 16

Accepted Solution

by:
sunithnair earned 1500 total points
ID: 23620661
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

0
 

Author Comment

by:mmalik15
ID: 23620739
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,
0
 
LVL 8

Expert Comment

by:SunnyDark
ID: 23620747
Try looking at the resulting HTML (after running the page->View Source) , this might give you a clue...
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23620858
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

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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