天天看點

c# winform TableLayoutPanel 動态 删除一行

動态删除 TableLayoutPanel 的某一行

說明:TableLayoutPanel 動态删除一行要修改三處地方:

  1. 删除該行單元格内的控件
  2. 删除該行樣式
  3. 設定行數量-1

如果隻删除了行樣式和行數量, 沒有删除裡面的控件,一樣達不到删除效果。另外,改行删除後,改行位置會變成空白。需要将後面的行依次向上移動。

private void tableDeleteRow(TableLayoutPanel tableLayoutPanel1, int row)
{
     int column = tableLayoutPanel1.ColumnCount;

     // 删除該行單元格中的控件
     Control control;
     for (int j = 0; j < column; j++)
     {
         control = tableLayoutPanel1.GetControlFromPosition(j, row);
         tableLayoutPanel1.Controls.Remove(control);
     }

      // 删除該行樣式
     tableLayoutPanel1.RowStyles.RemoveAt(row);
     // 行數設定 - 1
     tableLayoutPanel1.RowCount = tableLayoutPanel1.RowCount - 1;
 }
           

參考:https://www.fengyunxiao.cn

繼續閱讀