tableLayoutPanel 和数据表绑定的复选框有问题吗?

Issue with tableLayoutPanel and datatable-bound CheckBoxes?

我有一个 Windows Form,在 tableLayoutPanel 中显示 checkboxes 的列表。

正在显示复选框,但它们之间的间距很奇怪。 tableLayoutPanelMsgs.ColumnCount 设置为 1。

这是我的代码:

DataTable messagesTable = ds.Tables[0];

foreach (DataRow row in messagesTable.Rows)
{
    CheckBox ck = new CheckBox();
    ck.Text = row[1].ToString();
    tableLayoutPanel1.Controls.Add(ck);
}

Windows 表单中的复选框是这样的。注意每个复选框之间的间距。

我该如何解决这个问题?再次感谢。

您需要设置RowStyle

见附件截图设置 Properties:

必须至少有一行,您需要设置它的样式。其余行将自动添加,默认样式为 AutoSize.

为添加的每个复选框调用 SetRow 以将其放置在 tableLayoutPanel1 内的单独行中:

DataTable messagesTable = ds.Tables[0];
int I = 0;

tableLayoutPanel1.RowCount = messagesTable.Rows.Count;

foreach (DataRow row in messagesTable.Rows)
{
    CheckBox ck = new CheckBox();
    ck.Text = row[1].ToString();
    tableLayoutPanel1.Controls.Add(ck);
    tableLayoutPanel1.SetRow(ck, i++);
}