Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i want to add one row at a time on button click and on each click a new row should generate.
i have write a code but it generate two rows at a time.the code is as follows:-

C#
private void btnaddrow_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            tableLayoutPanel1.AutoSize = true;

            int InsertRowNum = tableLayoutPanel1.RowCount;
            tableLayoutPanel1.AutoScroll = true;
            tableLayoutPanel1.Visible = true;

            RowStyle newRowStyle = new RowStyle();
            newRowStyle.SizeType = SizeType.Absolute;
            newRowStyle.Height = 30;
            tableLayoutPanel1.RowStyles.Insert(InsertRowNum, newRowStyle);

            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(SizeType.Absolute, 30));
            // Add controls to the new row
            for (int row = 0; row < tableLayoutPanel1.RowCount; row++)
            {
                for (int column = 0; column < tableLayoutPanel1.ColumnCount; column++)
                {
                    TextBox textBox = new TextBox();
                    textBox.Text = string.Format("TextBox({0}, {1}) ", column, row);
                    tableLayoutPanel1.Controls.Add(textBox, column, row);
                }
                
            }
            

            tableLayoutPanel1.TabIndex++;
            tableLayoutPanel1.Visible = true;


        }
Thank You.
Posted
Updated 10-Aug-11 0:16am
v4

1 solution

AFAIK, TableLayoutPanel does not provide anything to do this.

Update:

You do not need to loop through the rows and columns. Just add controls and they will be placed in the consecutive cells. Panel will automatically add new rows if needed.
   
v2
Comments
SURBHI TYAGI 10-Aug-11 5:50am
   
how can you say it?I have done a code for but it create only one row./if i click again on the button than its not working.
lw@zi 10-Aug-11 5:53am
   
Can you update the question with the code you have written?
SURBHI TYAGI 10-Aug-11 6:07am
   
ya.i had.
lw@zi 10-Aug-11 6:19am
   
Updated the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900