用动态添加每一条记录的方式制作datagridview效率很低
谁能帮我看看,为什么这么慢,方法不对?有没有其他办法能直接更改datagridview布局(以前发了一个贴问大家都说只能自己做,那我这个做的方法不对了?)
private void huifu()
{
DataTable hj = shuju.ds("。。。。。。");
int tiaoshu = 0;
tiaoshu = shuju.tiaoshu("。。。。。。");
for (int i = 0; i < tiaoshu; i++)
{
TableLayoutPanel tlp1 = new TableLayoutPanel();
tlp1.Dock = DockStyle.Top;
tlp1.ColumnCount = 1;
tlp1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
tlp1.RowCount = 2;
tlp1.AutoSize = true;
tlp1.RowStyles.Add(new RowStyle());
tlp1.RowStyles.Add(new RowStyle());
Label lb1 = new Label();
Label lb2 = new Label();
lb1.Margin = new Padding(3);
lb2.Margin = new Padding(3);
lb1.AutoSize = true;
lb2.AutoSize = true;
lb1.Dock = DockStyle.Fill;
lb1.TextAlign = ContentAlignment.MiddleLeft;
lb2.Dock = DockStyle.Fill;
lb2.TextAlign = ContentAlignment.MiddleRight;
lb1.Text = Convert.ToString(hj.Rows[i].ItemArray.GetValue(1));
lb2.Text = Convert.ToString(hj.Rows[i].ItemArray.GetValue(0)) + " " + Convert.ToString(hj.Rows[i].ItemArray.GetValue(2));
tlp1.Controls.Add(lb1, 0, 0);
tlp1.Controls.Add(lb2, 0, 1);
tlp1.Paint += new PaintEventHandler(hf_Paint);
Controls.Add(tlp1);
}
}
------解决方案--------------------
设置布局时,可以先挂起布局,然后在恢复
dataGridView1.SuspendLayout();
//这里处理布局
dataGridView1.ResumeLayout();
------解决方案--------------------
你把 TableLayoutPanel 合并,有几条就加几行,现在一个才一行,太浪费了