日期:2014-05-20  浏览次数:20451 次

|M| 如何在动态的复制DataGrid
我页面中有一个DataGrid   dg1
现在要求是在页面中动态添加两个DataGrid
样式和各种内容和这个dg1完全相同

谢谢

------解决方案--------------------
System.Web.UI.WebControls.DataGrid DataGrid1 = new DataGrid();
// DataGrid1.ShowHeader = false;

DataGrid1.AutoGenerateColumns = false;
DataGrid1.DataSource = db.RetrieveSet(sql).Tables[0];

BoundColumn dgc_id8 = new BoundColumn();
dgc_id8.DataField = "TelNum ";
dgc_id8.HeaderText = "电话号码 ";
dgc_id8.ItemStyle.Wrap = false;
DataGrid1.Columns.Add(dgc_id8);

BoundColumn dgc_id1 = new BoundColumn();
dgc_id1.DataField = "CallNum ";
dgc_id1.HeaderText = "被叫号码 ";
dgc_id1.ItemStyle.Wrap = false;
DataGrid1.Columns.Add(dgc_id1);

BoundColumn dgc_id2= new BoundColumn();
dgc_id2.DataField = "StartTime ";
dgc_id2.HeaderText = "开始时间 ";
DataGrid1.Columns.Add(dgc_id2);

BoundColumn dgc_id3 = new BoundColumn();
dgc_id3.DataField = "EndTime ";
dgc_id3.HeaderText = "结束时间 ";
DataGrid1.Columns.Add(dgc_id3);

BoundColumn dgc_id4 = new BoundColumn();
dgc_id4.DataField = "UseTime ";
dgc_id4.HeaderText = "用时 ";
DataGrid1.Columns.Add(dgc_id4);

BoundColumn dgc_id6 = new BoundColumn();
dgc_id6.DataField = "RateType ";
dgc_id6.HeaderText = "计费类型 ";
DataGrid1.Columns.Add(dgc_id6);

BoundColumn dgc_id7 = new BoundColumn();
dgc_id7.DataField = "Rate ";
dgc_id7.HeaderText = "费率 ";
DataGrid1.Columns.Add(dgc_id7);

BoundColumn dgc_id5 = new BoundColumn();
dgc_id5.DataField = "Spend ";
dgc_id5.HeaderText = "费用 ";
DataGrid1.Columns.Add(dgc_id5);

DataGrid1.DataBind();
DataGrid1.Style.Add( "left ", "132px ");
DataGrid1.Style.Add( "TOP ", "144px ");
DataGrid1.Width = 500;

DataGrid1.Style[ "BorderStyle "]= "None ";
DataGrid1.Attributes.Add( "BorderStyle ", "None ");

DataGrid1.Attributes.Add( "BorderWidth ", "1px ");

DataGrid1.AutoGenerateColumns=true;


DataGrid1.SelectedItemStyle.Font.Bold=true;
DataGrid1.SelectedItemStyle.ForeColor=Color.White;

DataGrid1.ItemStyle.HorizontalAlign=HorizontalAlign.Center;


DataGrid1.ItemStyle.Wrap=true;


DataGrid1.HeaderStyle.Font.Bold=true;
DataGrid1.HeaderStyle.ForeColor=Color.White;
DataGrid1.HeaderStyle.BackColor=Color.FromName( "#B2C8C0 ");
DataGrid1.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
DataGrid1.HeaderStyle.Wrap=false;


DataGrid1.AutoGenerateColumns=false;
DataGrid1.HorizontalAlign=HorizontalAlign.Center;


DataGrid1.PagerStyle.HorizontalAlign=HorizontalAlign.Center;
DataGrid1.PagerStyle.Wrap=true;
DataGrid1.Width = new Unit(800);

this.Controls.Add(DataGrid1);
------解决方案--------------------
新的DataGrid要分页吗?

------解决方案--------------------
具体什么问题没有说清楚,如果需要另外两个同样的控件的话可以直接复制前台空间的代码更改下id并在后台邦定义下就可以了
------解决方案--------------------