0

This code add last row in the Table . I mean just last record get displayed I thought its a problem of viewstate so tried to EnableViewState but it says that its not like that

        Employee emp = new Employee();
        Table tb = new Table();
        TableRow tr1 = new TableRow();
        TableCell tc1 = new TableCell();
        TableCell tc2 = new TableCell();
        TableCell tc3 = new TableCell();


  for(int i=0;i<7;i++){
    emp = con.GetNextEmployee();
      if (emp != null)
      {
       tc1.Text = emp.name;
       tc2.Text = emp.position;
       tc3.Text = emp.ext;
       tr1.Cells.Add(tc1);
       tr1.Cells.Add(tc2);
       tr1.Cells.Add(tc3);
       tb.Rows.Add(tr1);
      }
   }
 Panel1.Controls.Add(tb);

Kindly help me

2 Answers 2

2

move these lines into the loop:

    TableRow tr1 = new TableRow();
    TableCell tc1 = new TableCell();
    TableCell tc2 = new TableCell();
    TableCell tc3 = new TableCell();

why part: actually you don't need to re-define them.

but the objects must be RE-created after you add them to your table.

because: when you add a row/cell to the table. row/cells' "reference" is being added to table.

so, second time you change the row or cell, actually, you are changing the row/cell "that is already in your table"

Sign up to request clarification or add additional context in comments.

4 Comments

answer is right but why is there a need to define the references every time
actually you don't need to re-define them. but the objects must be RE-created after you add them to your table. because: when you add a row/cell to the table. row/cells' "reference" is being added to table. so, second time you change the row or cell, actually, you are changing the row/cell "that is already in your table"
so the total count of 'table' object made will always remain one all the time because its not a new reference ,it is just re created Thanks
yes. when you re create the row/cell object there will be completely new row in the stack memory. otherwise you will always work on the same row.
0

You can do like this:

Table tbl = new Table();

TableRow tr = new TableRow();
TableCell tc = new TableCell();

for(int i=0;i<count;i++)
{
tr = new TableRow();
tc = new TableCell();

tc.Text = "Your Cell Text";
tr.Cells.Add(tc);

tc = new TableCell();
//Add new cell and add code to your cell
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.