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

标签 c# database

我正在使用 C# 开发 Windows 应用程序。我创建了一个 Windows 窗体,因为我添加了一个表格面板控件。在数据库中,我有一个表格,它有 4 列,如书名、书籍图像、书籍类别和书籍子类别。现在我在一个表中有 10 条记录。

我想在表格面板中显示所有这些数据。我尝试了以下代码。但我没有得到正确的输出。我必须添加一个图片框控件和三个标签控件,即我必须创建 4 列,所以 Column 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);
            }
        }
    }

请给我建议任何解决方案。提前致谢。

最佳答案

您正在将所有四个控件添加到您的每个单元格中,因为您执行了

            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;
        }

关于c# - 如何在表格布局面板中动态添加行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676337/

相关文章:

c# - 使用参数调用方法会出错

android - 将文件从 Assets 复制到数据文件夹后,数据未首次在 android 中显示

c# - 你能在 T 是任何类型的地方捕获异常吗?

database - 如何从 Informix 中的名称获取约束详细信息?

database - 以编程方式从 edmx 实体 3.5 文件创建数据库

linux - 提交后启动时Docker Oracle数据库失败

mysql - 使用连接创建 View

c# - 如何使这些结构函数通用?

c# - 如何在 Windows 10 UWP 中正确实现语音识别

c# - 无需调用即可从 lambda 获取方法名称