分享

gridview嵌套gridview的方法

 悟静 2012-01-11

gridview嵌套gridview的方法  

2008-12-23 11:29:51|  分类: 默认分类 |字号 订阅

网上也有关于gridview的嵌套方法,感觉不灵活.所以自己写一个.

*.aspx 代码 很简单

<asp:GridView ID="GridView1" runat="server" Width="100%" OnRowDataBound="GridView1_RowDataBound">

</asp:GridView>

*.aspx.cs 多余的代码已经去了.

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            GridView1.DataSource = CreateList("钢铁");

            GridView1.DataBind();

        }

    }

   ///外层gv行绑定

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowIndex > -1)

        {

            foreach (TableCell cell in e.Row.Cells)

            {

                cell.Controls.Add(GetGridView(cell.Text));

            }

        }

    }

    //第二个GridView

    private GridView GetGridView(string Text)

    {

        GridView gv = new GridView();

        gv.ID = string.Concat(Text, "id");

        gv.DataSource = CreateList(Text);

        gv.DataBind();

        gv.HeaderRow.Visible = false;

        gv.Font.Size = FontUnit.Parse("12px");

        gv.Width = Unit.Parse("100%");

        //gv.RowDataBound += event;  再往下嵌套.....

        return gv;

    }

    //记录集

    public ICollection CreateList(string Text)

    {

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("一季度", typeof(System.String)));

        dt.Columns.Add(new DataColumn("二季度", typeof(System.String)));

        DataRow dr;

        for (int i = 0; i < 10; i++)

        {

            Random r = new Random();

            dr = dt.NewRow();

            dr[0] = i.ToString() + "月";

            dr[1] = r.Next(99).ToString() + "%";

            dt.Rows.Add(dr);

        }

        return new System.Data.DataView(dt);

    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多