TableLayoutPanel: Delete Rows

13,133

Solution 1

tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowCount = 0;

Solution 2

I Googled and Binged a solution to this problem for DAYS and couldn't find one. I finally came with a solution that works!

tableLayoutPanel.SuspendLayout();

while (tableLayoutPanel.RowCount > 1)
{
    int row = tableLayoutPanel.RowCount - 1;
    for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
    {
        Control c = tableLayoutPanel.GetControlFromPosition(i, row);
        tableLayoutPanel.Controls.Remove(c);
        c.Dispose();
    }

    tableLayoutPanel.RowStyles.RemoveAt(row);
    tableLayoutPanel.RowCount--;
}

tableLayoutPanel.ResumeLayout(false);
tableLayoutPanel.PerformLayout();

In my solution, I do not want to remove the first row.

Solution 3

OK, looking at your second edit, I deleted my answer and am adding this new one.

I really doubt this works. Your while loop executes forever.

int c = UrlRows.Length - 1;
while (c <= UrlRows.Length - 1) //C will decrement forever and always be less than or equal
{
    TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
    c--;
}

I'm not terribly sure what you want to do in that method, if you mean to remove everything, what you had initially would have worked.

int c = 1;
while (c <= UrlRows.Length - 1) //You now loop through all elements in TBP
{
    TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
    c++;
}
Share:
13,133
gbbb
Author by

gbbb

Updated on June 04, 2022

Comments

  • gbbb
    gbbb almost 2 years

    I have a TableLayoutPanel which is filled with rows at runtime using a text file (get each row from the text file, and put it in cells contained in new rows). Code Looks like this:

    public static string UrlList= @"C:\Users\Berisha\Desktop\URLs.txt";
    string[] UrlRows = System.IO.File.ReadAllLines(@UrlList);
            private void InitPaths()
        {
            int a = 0;
            int c = 1;
            while (a < UrlRows.Length-1)
            {   
                //new label
                var label = new Label();
                label.Dock = DockStyle.Fill;
                label.AutoSize = false;
                label.Text = UrlRows[a];
                label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                label.Size = new System.Drawing.Size(22, 13);
                label.BackColor = System.Drawing.Color.Transparent;
                TBP.Controls.Add(label, 3, c); //Add to TableLayoutPanel
                a++;
                c++;
            }
        }
    

    Although I want to be able to manually edit the source, so I wrote a method which would delete everything new created, but seem to be stuck here, because it doesn't work:

            private void clearPaths()
        {   
            int c = UrlRows.Length - 1;
            while (c <= UrlRows.Length - 1)
            {
                TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
                c--;
            }
    
        }
    

    //The Code Stops at: TableLayoutPanel.RowStyles.RemoveAt(c);(while Debugging) //and the error reads : "Object reference not set to an instance of an object" Update: I managed to get out of the Error, my Problem now, after I say RemoveAt, nothing seems to be removed Does anybody know what it is that I could do?