• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3253
  • Last Modified:

Add Rows to Dynamically generated table

I have a table that I am building dynamically depending on a specified number of rows set by the user. What I want to do is to add more rows if they need more. For example I will have a button on the form that says add 10 more rows. I need to add these rows at the bottom of the already generated table. The CreateTable() function below is what first builds the table based on a number of rows the user specified. The btnSubmit_Click() event handler would append the additional 10 rows. I tried using  tblThrows.Rows.AddAt(?,tr); but when I do a row count it is only returning 1, it does not see the dynamically generated rows.  Any help would be greatly appreciated.
public void CreateTable(int Rows)
    {
 
        for (int i = 1; i <= Rows; i++)
        {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            TableCell td1 = new TableCell();
            TextBox txtTicketsWon = new TextBox();
 
            txtTicketsWon.ID = i + "_TicketsWon";
            txtTicketsWon.Attributes.Add("onblur", "AddTickets()"); 
 
            tr.ID = i + "_Row";
 
            td.BorderWidth = 1;
            td.BorderStyle = BorderStyle.Solid;
            td.BorderColor = Color.Black;
 
            td1.BorderWidth = 1;
            td1.BorderStyle = BorderStyle.Solid;
            td1.BorderColor = Color.Black;
 
            td.Text = i.ToString();
            td1.Controls.Add(txtTicketsWon);
            tr.Cells.Add(td);
            tr.Cells.Add(td1);
 
            tblThrows.Rows.Add(tr);
            RowCount += 1;
        }
 
    }
 
    public void btnSubmit_Click(object o, EventArgs e)
    {
 
        int Count = tblThrows.Rows.Count;
        
        for (int i = 0; i <= 10; i++)
        {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            TableCell td1 = new TableCell();
            TextBox txtTicketsWon = new TextBox();
 
            txtTicketsWon.ID = i + "Additional_TicketsWon";
 
            td.BorderWidth = 1;
            td.BorderStyle = BorderStyle.Solid;
            td.BorderColor = Color.Black;
 
            td1.BorderWidth = 1;
            td1.BorderStyle = BorderStyle.Solid;
            td1.BorderColor = Color.Black;
 
            td.Text = i.ToString();
            td1.Controls.Add(txtTicketsWon);
            tr.Cells.Add(td);
            tr.Cells.Add(td1);
 
            tblThrows.Rows.AddAt(?,tr);
        }
 
    }

Open in new window

0
pablo_munoz
Asked:
pablo_munoz
1 Solution
 
Bob LearnedCommented:
I would suggest refactoring the code within the for loop in the CreateTable, and call that method to add to the table.

Bob
0
 
pablo_munozAuthor Commented:
           for(var i = 0; i < 10; i++)
            {
                var tbl = document.getElementById('tblThrows');
                var lastRow = tbl.rows.length;
               
                // if there's no header row in the table, then iteration = lastRow + 1
                var iteration = lastRow;
                var row = tbl.insertRow(lastRow);

                // left cell
                var cellLeft = row.insertCell(0);
                var textNode = document.createTextNode(iteration);
                cellLeft.style.border = "1px solid #000000";
                cellLeft.appendChild(textNode);

                // right cell
                var cellRight = row.insertCell(1);
                var el = document.createElement('input');
                el.type = 'text';
                el.name = 'txtRow' + iteration;
                el.id = 'txtRow' + iteration;
                el.size = 20;

                el.onblur = AddTickets;
                cellRight.style.border = "1px solid #000000";
                cellRight.appendChild(el);
            }  
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now