程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> GridView實現動態讀取子表數據

GridView實現動態讀取子表數據

編輯:.NET實例教程

前台頁面代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestTwoTable.ASPx.cs" Inherits="Test_TestTwoTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">

<html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ASP:GridView ID="GridVIEw1" runat="server" AutoGenerateColumns="False" Font-Size="14px"
           Width="589px" OnRowCreated="GridView1_RowCreated" OnSelectedIndexChanging="GridVIEw1_SelectedIndexChanging">
            <Columns>
                <ASP:CommandFIEld ButtonType="Button" ShowSelectButton="True" />
                <ASP:BoundField DataFIEld="Code" HeaderText="Code" />
                <ASP:BoundField DataFIEld="FName" HeaderText="FName" />
                <ASP:BoundField DataFIEld="MName" HeaderText="MName" />
                <ASP:BoundField DataFIEld="LName" HeaderText="LName" />
                <ASP:BoundField DataFIEld="Age" HeaderText="Age&qu

$False$

ot; />
                <ASP:BoundField DataFIEld="Elementary" HeaderText="Elementary" />
                <ASP:TemplateFIEld>
                    <AlternatingItemTemplate>
                        <ASP:GridView ID="GridVIEw2" runat="server" Width="555px" >
                        </ASP:GridVIEw>
                    </AlternatingItemTemplate>
                </ASP:TemplateFIEld>
            </Columns>
        </ASP:GridVIEw>
   
    </div>
    </form>
</body>
</Html>

 後台頁面代碼:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test_TestTwoTable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.MaintainScrollPositionOnPostBack = true;
        if (!this.IsPostBack)
        {
            this.GridVIEw1.DataSource = GetData();
&nbsp;           this.GridVIEw1.DataBind();
        }
    }
    //獲取數據部分
    private DataTable GetData()
    {
        DataTable dt;
        dt = new DataTable();

        dt.Columns.Add("Code");
        dt.Columns.Add("FName");
        dt.Columns.Add("MName");
        dt.Columns.Add("LName");
        dt.Columns.Add("Age");
        dt.Columns.Add("Elementary");
        dt.Columns.Add("Junior");
        dt.Columns.Add("High");
        dt.Columns.Add("Religion");

        for (int i = 1; i <= 12; i++)
        {
            DataRow dr = dt.NewRow();

            dr["Code"] = "Code" + i.ToString();
            dr["FName"] = "FName" + i.ToString();
            dr["MName"] = "MName" + i.ToString();
            dr["LName"] = "LName" + i.ToString();
            dr["Age"] = "Age" + i.ToString();
            dr["Elementary"] = "Elementary" + i.ToString();
            dr["Junior"] = "Junior" + i.ToString();
            dr["High"] = "High" + i.ToString();
            dr["Religion"] = "Religion" + i.ToString();

            dt.Rows.Add(dr);
        }

        //為了實現自動隱藏功能,要在每個行的下面添加一個行,用於自動隱藏自表
        DataTable OkTable = new DataTable();
        OkTable.Merge(dt);//獲取表結構和數據
        OkTable.Rows.Clear();
        //重新填充數據
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow 有效行 = OkTable.NewRow();

            有效行["Code"] = dt.Rows[i]["Code"].ToString();
            有效行["FName"] = dt.Rows[i]["FName"].ToString();
            有效行["MName"] = dt.Rows[i]["MName"].ToString();
            有效行["LName"] = dt.Rows[i]["LName"].ToString();
            有效行["Age"] = dt.Rows[i]["Age"].ToString();
            有效行["Elementary"] = dt.Rows[i]["Elementary"].ToString();
            有效行["Junior"] = dt.Rows[i]["Junior"].ToString();
            有效行["High"] = dt.Rows[i]["High"].ToString();
            有效行["Religion"] = dt.Rows[i]["Religion"].ToString();

            OkTable.Rows.Add(有效行);

            DataRow 無效行 = OkTable.NewRow();
            無效行["Code"] = "0";
            無效行["FName"] = "0";
            無效行["MName"] = "0";
            無效行["LName"] = "0";
            無效行["Age"] = "0";
            無效行["Elementary"] = "0";
            無效行["Junior"] = "0";
            無效行["High"] = "0";
            無效行["Religion"] = "0";

            OkTable.Rows.Add(無效行);

        }
        return OkTable;
    }
    //一定要在這個事件中重寫,不然當回返後所重寫的就會失效
    int j = 0;
    protected void GridView1_RowCreated(object sender, GridVIEwRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            j = j + 1;
            if (j % 2 == 0)
            {
                e.Row.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
&n           ;  e.Row.Visible = false;
            }
        }
    }
    //所用重寫的事件方法
    public void NewRenderMethod(HtmlTextWriter writer, Control ctl)
    {
        writer.Write("<TD >&nbsp;</TD>\n");
        TableCell cell = (TableCell)ctl.Controls[7];
        cell.Attributes.Add("colspan", "7");
        cell.Attributes.Add("align", "center");
        cell.RenderControl(writer);
    }
    //選擇按扭
    protected void GridView1_SelectedIndexChanging(object sender, GridVIEwSelectEventArgs e)
    {
        GridViewRow ChildRow = GridVIEw1.Rows[e.NewSelectedIndex + 1];
        if (ChildRow.Visible)
        {
            ChildRow.Visible = false;
        }
        else
        {
            ChildRow.Visible = true;
        }

        GridViewRow SelectedRow = GridVIEw1.Rows[e.NewSelectedIndex];
        string ParentID = SelectedRow.Cells[1].Text;

        Response.Write(ParentID);
        GridVIEw g;
        g = (GridView)ChildRow.FindControl("GridVIEw2");

        if (g == null)
        {
            Response.Write(";沒查找到GridVIEw");
        }
        else
        {
            g.DataSource = this.GetChildData(ParentID); //模擬字表的內容
            g.DataBind();
        }
    }
    //模擬字表的內容
    private DataTable GetChildData(string ParentID)
    {
        DataTable dt;
        dt = new DataTable();

        dt.Columns.Add("Age");
        dt.Columns.Add("Elementary");
        dt.Columns.Add("Junior");
        dt.Columns.Add("High");
        dt.Columns.Add("Religion");

        for (int i = 1; i <= 12; i++)
        {
            DataRow dr = dt.NewRow();

            dr["Age"] = "Age" + i.ToString();
            dr["Elementary"] = "Elementary" + i.ToString();
            dr["Junior"] = "Junior" + i.ToString();
            dr["High"] = "High" + i.ToString();
            dr["Religion"] = "Religion" + i.ToString();

            dt.Rows.Add(dr);
        }
        return dt;
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved