C# 如何获取特定行以插入控件

C# 如何获取特定行以插入控件,c#,tablelayoutpanel,C#,Tablelayoutpanel,我试图在c#tableLayoutPanel中的确定位置插入一行 我需要检查其中一个标签中不包含复选标记的第一行,并将其设置为要插入控件的行 尽管我认为我在for循环中设置了insertRow,但当它到达插入行的点时,insertRow值为0。似乎当for循环退出时,insertRow值恢复为0。如果我没有用“=0”初始化insertRow,那么当我收到关于使用未分配变量的错误消息时。我真的很感激任何帮助 int insertRow = 0; int rowCou

我试图在c#tableLayoutPanel中的确定位置插入一行

我需要检查其中一个标签中不包含复选标记的第一行,并将其设置为要插入控件的行

尽管我认为我在for循环中设置了insertRow,但当它到达插入行的点时,insertRow值为0。似乎当for循环退出时,insertRow值恢复为0。如果我没有用“=0”初始化insertRow,那么当我收到关于使用未分配变量的错误消息时。我真的很感激任何帮助

        int insertRow = 0;
        int rowCount = tableLayoutPanel2.RowCount;

        //Get first row without member listed
        for (int i = 1; i <= rowCount - 1; i++)
        {
            if (tableLayoutPanel2.GetControlFromPosition(1, i).Text != "✔")
            {
                insertRow = i;
            }
        }
        //Add Row to table
        tableLayoutPanel2.RowCount++;
        tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29));
        //Insert Row
        tableLayoutPanel2.Controls.Add(new Label() { Text = Name.Text, TextAlign = ContentAlignment.MiddleCenter, Anchor = AnchorStyles.None, AutoSize = false }, 0, insertRow);
int insertRow=0;
int rowCount=tableLayoutPanel2.rowCount;
//获取未列出成员的第一行

对于(inti=1;i结果表明,我所需要的只是“中断”循环以保持insertRow变量的设置

for (int i = 1; i <= rowCount - 1; i++)
        {
            if (tableLayoutPanel2.GetControlFromPosition(1, i).Text != "✔")
            {
                insertRow = i;
                break;
            }
        }
        //Add Row to table
        tableLayoutPanel2.RowCount++;
        tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29));
        //Insert member
        tableLayoutPanel2.Controls.Add(new Label() { Text = Name.Text, TextAlign = ContentAlignment.MiddleCenter, Anchor = AnchorStyles.None, AutoSize = false }, 0, insertRow);

用于(int i=1;我已经尝试过单步执行代码了?为什么从第二行开始?行和列索引都是从零开始的。您也只查看第二列。当我设置断点时,这就是为什么我知道insertRow的值没有保留。它在for循环期间递增,但一旦for循环完成,它就会返回到零。我从第二行开始,因为第一行只是列标题。(我知道值不会包含在那里。)复选标记将只在第二列中。请看,其中一致意见是“不,他们不应该”!