首页 > 开发 > 综合 > 正文

嵌套的DataGrid如何为子DataGrid动态增加模板列

2024-07-21 02:15:59
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!

例子比较简单,直接贴代码了,例子达到以下功能:
(1)两个datagrid嵌套
(2)外面一个datagrid分页
(3)里面一个datagrid动态增加模板列 

<%@ page language="c#" codebehind="webform45.aspx.cs" autoeventwireup="false" inherits="csdn2.webform45" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
    <head>
        <title>test</title>
        <meta content="microsoft visual studio .net 7.1" name="generator">
        <meta content="c#" name="code_language">
        <meta content="javascript" name="vs_defaultclientscript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
    </head>
    <body>
        <form id="form1" method="post" runat="server">
            <asp:datagrid id="datagrid1" runat="server" autogeneratecolumns="false" pagesize="2" allowpaging="true">
                <columns>
                    <asp:templatecolumn headertext="分类名">
                        <itemtemplate>
                            <%# databinder.eval(container.dataitem, "classname") %>
                            <asp:datagrid id="datagrid2" runat="server" autogeneratecolumns="false"></asp:datagrid>
                        </itemtemplate>
                    </asp:templatecolumn>
                </columns>
            </asp:datagrid></form>
    </body>
</html>


 

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace csdn2
{
    /**//// <summary>
    /// webform45 的摘要说明。
    /// </summary>
    public class webform45 : system.web.ui.page
    {
        protected system.web.ui.webcontrols.datagrid datagrid1;
    
        private void page_load(object sender, system.eventargs e)
        {
            // 在此处放置用户代码以初始化页面
            if(!ispostback)
            {
                setbind();
            }
        }

        private void setbind()
        {
            sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings["conn"]);
            sqldataadapter da=new sqldataadapter("select * from class;select * from topic",conn);
            dataset ds=new dataset();
            da.fill(ds);
            ds.relations.add("class_topic",ds.tables[0].columns["classid"],ds.tables[1].columns["topicclassid"]);
            this.datagrid1.datasource=ds.tables[0];
            this.datagrid1.databind();
        }

        web 窗体设计器生成的代码#region web 窗体设计器生成的代码
        override protected void oninit(eventargs e)
        {
            //
            // codegen: 该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
        
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {    
            this.datagrid1.pageindexchanged += new system.web.ui.webcontrols.datagridpagechangedeventhandler(this.datagrid1_pageindexchanged);
            this.datagrid1.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datagrid1_itemdatabound);
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

        private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
        {
            if(e.item.itemtype==listitemtype.item||e.item.itemtype==listitemtype.alternatingitem)
            {
                datagrid dgrd=(datagrid)e.item.findcontrol("datagrid2");
                dgrd.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datagrid2_itemdatabound);
                templatecolumn tm=new templatecolumn(); 
                tm.itemtemplate=new columntemplate(); 
                tm.headertext="主题名"; 
                dgrd.columns.add(tm); 
                dgrd.datasource=((datarowview)e.item.dataitem).row.getchildrows("class_topic");
                dgrd.databind();
            }
        }

        private void datagrid1_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
        {
            this.datagrid1.currentpageindex=e.newpageindex;
            setbind();
        
        }

        private void datagrid2_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
        {
            if(e.item.itemtype==listitemtype.item||e.item.itemtype==listitemtype.alternatingitem)
            {
                ((label)e.item.findcontrol("topicname")).text=convert.tostring(databinder.eval(e.item.dataitem,"[/"topicname/"]"));
            }
        }
    }

    public class columntemplate : itemplate 
    
        public void instantiatein(control container)       
        
            label l=new label(); 
            l.id="topicname";
            container.controls.add(l); 
        } 
    } 
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表