【发布时间】:2021-05-07 00:32:02
【问题描述】:
我需要创建一个表格,该表格将始终在 TableLayoutPanel 上显示 CTLog 的最后十条记录。因此,每当用户通过单击按钮在 Access 数据库中添加新的 CTLog 时,表格将动态更新并显示最后十个 CTLog。添加前十条记录时,我设法将它们放在表上,但是在第 10 行之后添加的那些记录无法显示。我使用了TableLayoutPanel上的旧标签替换的方法,删除旧标签,然后添加新标签。
private void RecentCT()
{
int j = 0;
for (j = 0; j < 10; j++)
{
tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(j + 1, 0));
tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(j + 1, 1));
}
string sql = "select Top 10 * from timer where ModelLog = @m and ShiftLog = @sl and ShiftStart = @ss and ShiftEnd = @se";
using (OleDbCommand cmd = new OleDbCommand(sql, connection))
{
//all cmd.Parameters.Add actions at here
try
{
connection.Open();
//List<string> results = new List<string>(); I used list and foreach previously
Label[] labels = new Label[10];
Label[] labels2 = new Label[10];
int i = 0;
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
labels[i] = new Label
{
Text = reader["CTLog"].ToString(),
Anchor = AnchorStyles.None,
Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular),
TextAlign = ContentAlignment.MiddleCenter
};
labels2[i] = new Label
{
Text = "Unit " + reader["UnitID"].ToString(),
Anchor = AnchorStyles.None,
Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular),
TextAlign = ContentAlignment.MiddleCenter
};
tableLayoutPanel1.Controls.Add(labels2[i], i + 1, 0);
tableLayoutPanel1.Controls.Add(labels[i], i + 1, 1);
i++;
}
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Recent cycle time records cannot be retrieved. Error: " + ex.Message);
connection.Close();
}
}
}
我是否遗漏了什么或者我的方法有什么问题?
【问题讨论】:
-
这是 DataGridView 的工作。
-
摆脱 LayoutPanel。这是垃圾。我尝试了很多次,但 LayoutPanel 中的属性非常有限,并且通常不起作用。通过创建主面板并将子面板添加到主面板,可以更轻松地创建您自己的 LayoutPanel。像这样单独滚动每个子面板,而不是在 LayoutPanel 中。
标签: c# winforms ms-access tablelayoutpanel