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

时间:2013-07-16 12:19:12

标签: c# database

我正在使用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 个答案:

答案 0 :(得分: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;
        }
相关问题