C#TableLayoutPanel使⽤⽅法
⼀、利⽤TableLayoutPanel类展⽰表格,以10⾏5列为例
第1步:在前台创建⼀个panel,使TableLayoutPanel对象填充其内部。
第2步:创建TableLayoutPanel类,其实例对象名为table
TableLayoutPanel table = new TableLayoutPanel();
第3步:设置列样式,循环显⽰⾏
private void Form2_Load(object sender, EventArgs e)
{
// 默认添加⼀⾏数据
table.Dock = DockStyle.Top;    //顶部填充
panel1.Controls.Add(table);
table.ColumnCount = 5;          //5列
table.Height = table.RowCount * 40; //table的整体⾼度,每⾏40
table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));    //利⽤百分⽐计算,0.2f表⽰占⽤本⾏长度的20%
table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));
tabletable
table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));
table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));
table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));
for (int i = 1; i <= 10; i++)
{
AddRow("苹果"+i.ToString().PadLeft(2,'0'),"橘⼦" + i.ToString().PadLeft(2, '0'), "⾹蕉" + i.ToString().PadLeft(2, '0'), "⾹⽠" + i.ToString().PadLeft(2, '0'), "⽢蔗" + i.ToString().PadLeft(2, '0'));    }
}
第4步:设置每⼀⾏样式
private void AddRow(string apple, string orange, string banana, string casaba, string sugarcane)
{
try
{
// 动态添加⼀⾏
table.RowCount++;
//设置⾼度,边框线也算⾼度,所以将40修改⼤⼀点
table.Height = table.RowCount * 44;
// ⾏⾼
table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
// 设置cell样式,增加线条
table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetPartial;
int i = table.RowCount - 1;
Label label1 = new Label();
label1.Text = apple;
label1.Width = 200;
label1.Height = 40;
label1.Font = new Font("⾪书", 13, FontStyle.Bold);
label1.TextAlign = ContentAlignment.MiddleCenter;
table.Controls.Add(label1, 0, i);
Label label2= new Label();
label2.Text = orange;
label2.Width = 200;
label2.Height = 40;
label2.Font = new Font("⾪书", 13, FontStyle.Bold);
label2.TextAlign = ContentAlignment.MiddleCenter;
table.Controls.Add(label2, 1, i);
Label label3 = new Label();
label3.Text = banana;
label3.Width = 200;
label3.Height = 40;
label3.Font = new Font("⾪书", 13, FontStyle.Bold);
label3.TextAlign = ContentAlignment.MiddleCenter;
table.Controls.Add(label3, 2, i);
Label label4 = new Label();
label4.Text = casaba;
label4.Width = 200;
label4.Height = 40;
label4.Font = new Font("⾪书", 13, FontStyle.Bold);
label4.TextAlign = ContentAlignment.MiddleCenter;
table.Controls.Add(label4, 3, i);
Label label5 = new Label();
label5.Text = sugarcane;
label5.Width = 200;
label5.Height = 40;
label5.Font = new Font("⾪书", 13, FontStyle.Bold);
label5.TextAlign = ContentAlignment.MiddleCenter;
table.Controls.Add(label5, 4, i);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.PadRight(30, ''), "提⽰", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
查询某⾏某列数据
private void button1_Click(object sender, EventArgs e) {
//查询第4⾏第4列数据,下标从0开始
int i = 3;
int j = 3;
Label label = (Label)table.Controls[i * 5 + j];
MessageBox.Show(label.Text);
}
修改某⾏某列数据
private void button2_Click(object sender, EventArgs e) {
//修改第6⾏第1列数据,下标从0开始
int i = 5;
int j = 0;
Label label = (Label)table.Controls[i * 5 + j];
label.Text = "已修改";
label.ForeColor = Color.Red;
}
1.单元格画线使⽤CellBorderStyle属性
⾸先在⼀⾏⼀列单元格内添加Panel控件,修改器属性ColumnSpan = 2 即可
好了,本⽂到此结束,哪⾥写的不对望读者指出~最后附上源码:
  链接:
  提取码: aih9

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。