当前位置: 代码迷 >> ASP.NET >> Gridview RowCreated 事件中动态加入control的有关问题
  详细解决方案

Gridview RowCreated 事件中动态加入control的有关问题

热度:8237   发布时间:2013-02-25 00:00:00.0
Gridview RowCreated 事件中动态加入control的问题
 

protected void gdvList_RowCreated(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  DropDownList dropdownList = new DropDownList();
  dropdownList.DataSource = addressList;
  dropdownList.DataTextField = "Name";
  dropdownList.DataValueField = "ID";
  dropdownList.DataBind();
  TableCell tc = new TableCell();
  tc.CssClass = "grid-content left";
  tc.Width = 60;
  tc.Controls.Add(dropdownList);
  e.Row.Cells.AddAt(3, tc);
  }
  }

问题是control能加在第3列,可是原有的cell向右移了一个,导致每行cell数量比gridview columns数量多一个。

如何替换当前cell,不是加一个。

------解决方案--------------------------------------------------------
你可以先把原先的删除掉
------解决方案--------------------------------------------------------
删除掉,然后再添加看下
------解决方案--------------------------------------------------------
e.Row.Cells.AddAt(3, tc)
改成
e.Row.Cells[3].Controls.Add(dropdownList);

不要增加 TableCell 了
------解决方案--------------------------------------------------------
探讨
e.Row.Cells.AddAt(3, tc)
改成
e.Row.Cells[3].Controls.Add(dropdownList);

不要增加 TableCell 了

------解决方案--------------------------------------------------------
其实,如果操作复杂,完全不用动态创建,你可以先放置一个,根据情况进行显示或者隐藏,或者Remove掉都可以
  相关解决方案