如何在表格布局面板中动态添加行和列

3
我正在使用C#开发Windows应用程序。我创建了一个窗体,在其中添加了一个表格面板控件。在数据库中,我有一个表,该表有4列,如书名、书籍图片、书籍类别和书籍子类别。现在我有10个记录在表中。
我想在表格面板中显示所有这些数据。我尝试了以下代码,但是我没有得到正确的输出。我需要添加一个图片框控件和三个标签控件,即我需要创建4列,因此第1列将具有图片框,其他三列将每个具有一个标签。我尝试的代码给出了输出,但它不正确。它在所有4列中显示图片框图像,然后是标签。
但是我希望显示的输出是,每一列都包含唯一的数据。
代码:
public void DynamicGenerateTable(int columnCount, int rowCount)
    {
        tableLayoutPanel1.Controls.Clear();
        //Clear out the existing row and column styles
        tableLayoutPanel1.ColumnStyles.Clear();
        tableLayoutPanel1.RowStyles.Clear();

        //Assign table no of rows and column
        tableLayoutPanel1.ColumnCount = columnCount;
        tableLayoutPanel1.RowCount = rowCount;
        WiCommonFunction.LoadCommonSettings();
        ShowInformation show = new ShowInformation();
        //ds = show.ShowBookImage();
        ds1 = show.ShowBookCategory();
        DataTable dt1 = ds1.Tables[0];

        for (int i = 0; i < columnCount; i++)
        {
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

            for (int j = 0; j < rowCount; j++)
            {
                if (i == 0)
                {
                    //defining the size of cell
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                }
                PictureBox picture = new PictureBox();
                picture.Size = new Size(220, 180);
                picture.SizeMode = PictureBoxSizeMode.StretchImage;
                Byte[] byteImage = (Byte[])(dt1.Rows[j]["BookImage"]);
                MemoryStream ms = new MemoryStream(byteImage);
                picture.Image = Image.FromStream(ms);

                Label lblCategory = new Label();
                lblCategory.Text = dt1.Rows[j]["CategoryName"].ToString();

                Label lblSubCategory = new Label();
                lblCategory.Text = dt1.Rows[j]["SubCategoryName"].ToString();

                Label lblBook = new Label();
                lblBook.Text = dt1.Rows[j]["BookName"].ToString();
                tableLayoutPanel1.Controls.Add(picture,i,j);
                tableLayoutPanel1.Controls.Add(lblCategory, i, j);
                tableLayoutPanel1.Controls.Add(lblSubCategory, i, j);
                tableLayoutPanel1.Controls.Add(lblBook, i, j);
            }
        }
    }

请给我提供任何解决方案。提前感谢。
1个回答

3
您将所有四个控件添加到每个单元格中,因为您执行了此操作。
            tableLayoutPanel1.Controls.Add(picture,i,j);
            tableLayoutPanel1.Controls.Add(lblCategory, i, j);
            tableLayoutPanel1.Controls.Add(lblSubCategory, i, j);
            tableLayoutPanel1.Controls.Add(lblBook, i, j);

对于每个 (i,j) 的组合,你需要一些类似于 switch 语句的东西,只将你想要添加到该单元格中的控件添加进去,例如:
        switch(i) {
          case 0:
            tableLayoutPanel1.Controls.Add(picture,i,j);
            break;
          case 1:
            tableLayoutPanel1.Controls.Add(lblCategory, i, j);
            break;
          case 2:
            tableLayoutPanel1.Controls.Add(lblSubCategory, i, j);
            break;
          case 3:
            tableLayoutPanel1.Controls.Add(lblBook, i, j);
            break;
        }

感谢您的回复。它对我很有帮助,我得到了我想要的输出。 - Ashwini Agivale

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接