我正在使用Windows C#。
首先,由于我的需要,那些不能改变的事情如下:
TableLayoutPanel
的大小是固定的。 现在,我想为所有行设置一个固定高度,但是随着行数的增加,如果我将
RowStyle
属性设置为Percent
与100.0F
一起使用,那么它可以很好地用于3到4个项目,但是在4-5个项目之后,可以对一行进行控制覆盖另一行上的控件。我已经搜索了很多,但是我找不到正确的答案。我也尝试了
AutoSize
的Percent
,Absolute
,RowStyle
属性,即使它不起作用。那么该怎么办?我怎样才能做到这一点?
最终,我想做与Windows C#的
DataGridView
相同的操作。提前致谢....
我正在使用WinForms ...示例代码在这里..
int cnt = tableLayout.RowCount = myDataTable.Rows.Count;
tableLayout.Size = new System.Drawing.Size(555, 200);
for (int i = 1; i <= cnt; i++)
{
Label lblSrNo = new Label();
lblSrNo.Text = i.ToString();
TextBox txt = new TextBox();
txt.Text = "";
txt.Size = new System.Drawing.Size(69, 20);
tableLayout.Controls.Add(lblSrNo, 0, i - 1);
tableLayout.Controls.Add(txt, 1, i - 1);
}
tableLayout.RowStyles.Clear();
foreach (RowStyle rs in tableLayout.RowStyles)
tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
标签和文本框对于4-5个#of行工作正常,但是每当#of行(在这种情况下,for循环中的变量cnt)增加时,这些行就会互相覆盖,这是一个控件覆盖另一个控件的...我拖放了TableLayoutPanel控件并手动创建了仅一行和两列。
所以,请告诉我该怎么做。
最佳答案
我本人还是tableLayoutPanels的新手,但我注意到在代码的底部,您正在清除集合中的所有行样式,然后尝试在foreach循环中遍历它们。
您这样做:
tableLayout.RowStyles.Clear(); //now you have zero rowstyles
foreach (RowStyle rs in tableLayout.RowStyles) //this will never execute
tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
试试这个吧。
TableLayoutRowStyleCollection styles =
tableLayout.RowStyles;
foreach (RowStyle style in styles){
// Set the row height to 20 pixels.
style.SizeType = SizeType.Absolute;
style.Height = 20;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
编辑:我只是意识到添加N行不会添加可以迭代的N行样式。我认为正在发生的事情是您要添加N行,但是它们都没有行样式。
我想您可以清除()行样式,然后添加N行样式,类似于您已经在做的事情。
关于c# - 修复TableLayoutPanel中每行的行高,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15004937/