심재운 블로그

- aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HierarchyDisplay_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:Table ID="Table1" runat="server">
            </asp:Table>
        </ItemTemplate>
        </asp:Repeater>
   
    </div>
    </form>
</body>
</html>


- cs

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;
using System.Data.SqlClient;

public partial class HierarchyDisplay_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            string sql = "Select * from Designation";
            SqlDataAdapter da = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            da.Fill(dt);

            DataSet Ds = new DataSet();
            Ds.Tables.Add(dt);


            DataColumn PriKey = new DataColumn();
            PriKey = dt.Columns["EmpNo"];
            DataColumn ForKey = new DataColumn();
            ForKey = dt.Columns["ReportsTo"];
            DataRelation dataRel = new DataRelation("ParentChild", PriKey, ForKey, false);
            dataRel.Nested = true;

            Ds.Relations.Add(dataRel);

            Repeater1.DataSource = GetParentObjects(Ds.Tables[0]);
            Repeater1.DataBind();
        }
    }

    private DataTable GetParentObjects(DataTable dtMain)
    {
        DataTable dtFinal = dtMain.Clone();

        foreach (DataRow parentRow in dtMain.Select("ReportsTo is null"))
        {
            dtFinal.ImportRow(parentRow);
            GetChildObjects(parentRow.GetChildRows("ParentChild"), ref dtFinal);
        }

        return dtFinal;
    }

    private void GetChildObjects(DataRow[] drChild, ref DataTable dtFinal)
    {
        foreach (DataRow childRow in drChild)
        {
            dtFinal.ImportRow(childRow);
            GetChildObjects(childRow.GetChildRows("ParentChild"), ref dtFinal);
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Table Table1 = (Table)e.Item.FindControl("Table1");
            Table1.Width = Unit.Percentage(30);
            Table1.BorderStyle = BorderStyle.Solid;
            Table1.BorderWidth = Unit.Pixel(1);
            Table1.CellPadding = 0;
            Table1.CellSpacing = 0;

            TableRow tblRow = new TableRow();
            TableCell tblEmptyCell = new TableCell();
            tblEmptyCell.Controls.Add(new LiteralControl(String.Empty));
            tblEmptyCell.Width = Unit.Percentage(20 * Convert.ToInt32(((DataRowView)(e.Item.DataItem))["Level"].ToString()));
            tblRow.Cells.Add(tblEmptyCell);

            TableCell tblCell = new TableCell();
            tblCell.Controls.Add(new LiteralControl(((DataRowView)(e.Item.DataItem))["Designation"].ToString()));
            tblCell.HorizontalAlign = HorizontalAlign.Left;
            tblRow.Cells.Add(tblCell);
            Table1.Rows.Add(tblRow);

        }
    }
}


트리형.... 테이블 구성





이 글을 공유합시다

facebook twitter kakaoTalk kakaostory naver band

본문과 관련 있는 내용으로 댓글을 남겨주시면 감사하겠습니다.

비밀글모드