首页 > 编程 > .NET > 正文

ASP.NET 2.0 中动态添加 GridView 模板列

2024-07-10 13:12:35
字体:
来源:转载
供稿:网友

  动态添加列,关键是实现 itemplate.instantiatein 方法。下面是一个添加 gridview 模板列的例子。

  c#代码

<%[email protected] page language="c#" %>
<%[email protected] import namespace="system.data" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
 "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<script runat="server">...
  icollection createdatasource()
  ...{
    datatable dt = new datatable();
    datarow dr;
    dt.columns.add(new datacolumn("id", typeof(int32)));
    dt.columns.add(new datacolumn("text", typeof(string)));
    for (int i = 0; i < 6; i++)
    ...{
      dr = dt.newrow();
      dr[0] = i;
      dr[1] = "列表项目 " + i.tostring();
      dt.rows.add(dr);
    }
    dataview dv = new dataview(dt);
    return dv;
  }

  public class gridviewtemplate : itemplate
  ...{
    private datacontrolrowtype templatetype;
    private string columnname;

    public gridviewtemplate( datacontrolrowtype type, string colname )
    ...{
      templatetype = type;
      columnname = colname;
    }

    public void instantiatein( system.web.ui.control container )
    ...{
      switch (templatetype)
      ...{
        case datacontrolrowtype.header:
          literal lc = new literal();
          lc.text = columnname;         
          container.controls.add(lc);         
          break;
        case datacontrolrowtype.datarow:
          dropdownlist drr = new dropdownlist();
          drr.id = "dropdown";
          drr.appenddatabounditems = true;
          drr.items.add(new listitem("-----请选择------",""));
          drr.items.add(new listitem("aa", "a"));
          drr.items.add(new listitem("bb", "b"));
          drr.items.add(new listitem("cc", "c"));
          container.controls.add(drr);
          break;
        default:
         break;
      }
    }
  }
 
  protected void page_load(object sender, eventargs e)
  ...{
    if (!ispostback)
    ...{
      templatefield customfield = new templatefield();
      customfield.showheader = true;
      customfield.headertemplate = new gridviewtemplate(datacontrolrowtype.header, "动态添加列");
      customfield.itemtemplate = new gridviewtemplate(datacontrolrowtype.datarow, "");
      gridview1.columns.add(customfield);
      gridview1.datasource = createdatasource();
      gridview1.databind();
    }
  }

  protected void gridview1_rowdatabound( object sender, gridviewroweventargs e )
  ...{
    if (e.row.rowtype == datacontrolrowtype.datarow)
    ...{
      //可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
      //下面只是例子,举一反三,不再费话了
      datarowview gv = (datarowview)e.row.dataitem;
      int itemseleted = int32.parse(gv.row["id"].tostring()) > 3 ? 0 : int32.parse(gv.row["id"].tostring());
      dropdownlist dr = (dropdownlist)e.row.findcontrol("dropdown");
      dr.selectedindex = itemseleted;
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
  <title>gridview动态添加模板列的例子</title>
</head>
<body>
<form id="form1" runat="server">
  <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"
     onrowdatabound="gridview1_rowdatabound">
    <columns>
      <asp:boundfield headertext="标题"  datafield="text"/>
    </columns>
  </asp:gridview>
</form>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表