2

I need to set the border color and border style to a table row that is dynamically created. How can i do that?

if (cmd.Connection.State == ConnectionState.Closed)
                cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())

            {

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {

                        JobDesignation = reader.GetString(0);
                        JobDescription = reader.GetString(1);
                        NoOfVacancies = Convert.ToString(reader.GetInt32(2));
                        DatePosted = Convert.ToString(reader.GetDateTime(3)).Replace("00:00:00", "");
                        jobId = reader.GetString(4);
                        int tblRows = 1;
                        int tblCols = 1;

                        Table tbl = new Table();
                        PlaceHolder1.Controls.Add(tbl);
                        for (int i = 0; i < tblRows; i++)
                        {
                            TableRow tr = new TableRow();

                            for (int j = 0; j < tblCols; j++)
                            {
                                TableCell tc = new TableCell();
                               System.Web.UI.WebControls.Label lblBox = new System.Web.UI.WebControls.Label();
                               lblBox .Text = "Job ID:" + jobId + Environment.NewLine + "Job Designation:" + JobDesignation + Environment.NewLine + "Job Description:" + JobDescription + Environment.NewLine + "Vacancies:" + NoOfVacancies + Environment.NewLine + "Ad Posted On:" + DatePosted + "";
                               tc.Controls.Add(lblBox);
                               tr.Cells.Add(tc);
                            }

                            tr.Width = new Unit("700px");
                            tr.Height = new Unit("200px");
                            tr.BorderColor = System.Drawing.Color.Black;
                            tr.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
                            tbl.Rows.Add(tr);

                        }
                        ViewState["dynamictable"] = true; 
                      } reader.NextResult();

                }

            }

I also want to display Job Id,Job Description, Job Designation,No Of vacancies in separate line. How can i achieve that?

Please help me.

Tannya
  • 161
  • 6
  • 21

5 Answers5

1

Displaying a DB table information dynamically ,without using any controls

http://www.dotnetfunda.com/Blogs/Venkyshwe8%40gmail.com/1264/displaying-a-table-information-dynamically-without-using-any-controls

Create a Table Dynamically in ASP.NET

http://www.dotnetcurry.com/ShowArticle.aspx?ID=135&AspxAutoDetectCookieSupport=1

Janki
  • 481
  • 2
  • 10
1

You can set property dynamically as below :

    TableRow row1 = new TableRow();
    row1.CssClass = "rowStyle1";

    TableCell cell1 = new TableCell();
    cell1.CssClass = "cellStyle1";


//create css class as below in your css file :

.rowStyle{
    border:1px solid red;
}
.cellStyle1{
    background-color:blue;
}
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
1

try with LiteralControl

for (int j = 0; j < tblCols; j++)
{
   TableCell tc = new TableCell();
   tc.Controls.Add(new LiteralControl("Job ID:" + jobId + "<br>" + "Job Designation:" + JobDesignation + "<br>" + "Job Description:" + JobDescription + "<br>" + "Vacancies:" + NoOfVacancies + "<br>" + "Ad Posted On:" + DatePosted + ""));
   tr.Cells.Add(tc);
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0

Well, if you are talking about style, I do strongly recommend you to use CSS. Please try to include the corresponding rows in a CSS class. In a separate CSS file, you can set the properties of that class as desired.

About your second question I would recommend you to create a new row with one cell and set its colspan to tblCols. You can do this right after:

tbl.Rows.Add(tr);

Hope that helps,

Ramon Araujo
  • 1,743
  • 22
  • 31
0

Since this is dynamic, I'm going to assume using the typical recommendations of defined styles in css classes won't do the job here - otherwise the simple solution is to set your css class (always the recommended approach)

In the case where they can't be pre-defined and can vary in style, Table inherits WebControl so you then have Table.Style which you can add all your style definitions to - ex: http://msdn.microsoft.com/en-us/library/system.web.ui.cssstylecollection.aspx

What happens though with your code above? What's your markup look like? TD can override TR border styles (ex row border color) but you aren't setting any above.

For your second question: To display on a separate line, use css for display:block so each item will show up on a new line - ex: How make 2 lines in <TD>

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71