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;
using System.Data;
namespace GridViewTreeView
{
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// 树图片路径
/// </summary>
public string TreeImagePath = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitList();
}
}
/// <summary>
/// 初始化列表
/// </summary>
private void InitList()
{
string connstr = "data source=. ;initial catalog=xgoss ;persist security info=False;user id=sa;password=123456;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
//ID 记录ID,DeptNo记录编号,SuperDeptID上级记录ID,DeptName永录名称.
SqlCommand comm1 = new SqlCommand("select ID,DeptNo,SuperDeptID,DeptName from officedba.deptInfo", conn);
SqlDataAdapter ad1 = new SqlDataAdapter(comm1);
DataSet ds = new DataSet();
ad1.Fill(ds, "Dept");
DataTable dt = ds.Tables["Dept"];
dt.Columns.Add("Level", typeof(System.Int16));//所属层次
dt.Columns.Add("HasChild", typeof(System.String));//是否有子结点
//设置顶级
foreach (DataRow DR in dt.Rows)
{
if (dt.Select("SuperDeptID=" + DR["ID"].ToString()).Length > 0)
DR["HasChild"] = 1;
else
DR["HasChild"] = 0;
if (DR["SuperDeptID"].ToString() == "")
DR["Level"] = 1;
}
DataTable adt = dt.Clone();
adt.Rows.Clear();
//记录层次排序
for (int i = 0; i < dt.Rows.Count ; i++)
{
int le = 1;
setLevel(dt, dt.Rows[i], ref le);
dt.Rows[i]["Level"] = le;
string sID = dt.Rows[i]["SuperDeptID"].ToString();
if (sID.Length ==0)
{
DataRow dr = adt.NewRow();
dr.ItemArray = dt.Rows[i].ItemArray;
adt.Rows.Add(dr);
}
if (sID.Length > 0)
{
int index = adt.Rows.IndexOf(adt.Select("ID=" + sID)[0]);
DataRow dr = adt.NewRow();
dr.ItemArray = dt.Rows[i].ItemArray;
//dt.Rows[i].Delete();
if (index > -1)
{
adt.Rows.InsertAt(dr, index+1);
}
else
{
adt.Rows.Add(dr);
}
}
}
gvList.DataSource = adt;
gvList.DataBind();
}
//设置记录所属层次
private void setLevel(DataTable dt, DataRow DR, ref int Le)
{
if (DR["SuperDeptID"].ToString() == "")
{
return;
}
else
{
if (dt.Select("ID=" + DR["SuperDeptID"].ToString()).Length > 0)
{
Le++;
setLevel(dt, dt.Select("ID=" + DR["SuperDeptID"].ToString())[0], ref Le);
}
}
}
/// <summary>
/// 行呈现处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
# region 生成树形结构
string id = gvList.DataKeys[e.Row.RowIndex]["ID"].ToString();
string pid = gvList.DataKeys[e.Row.RowIndex]["SuperDeptID"].ToString();
string level = gvList.DataKeys[e.Row.RowIndex]["Level"].ToString();
//增加节点的属性
e.Row.Attributes.Add("id", id);
e.Row.Attributes.Add("pid", pid);
e.Row.Attributes.Add("level", level);
e.Row.Attributes.Add("expand", "1");
int indent = (int.Parse(level) - 1) * 20;
//判断是否有子节点
if (gvList.DataKeys[e.Row.RowIndex]["HasChild"].ToString() == "1")
{
//设置父节点前面的图片和点击事件
HtmlImage img = new HtmlImage();
img.Src = TreeImagePath + "minimize.gif";
img.Attributes.Add("onclick", "setExpand(this)");
img.Style.Add("cursor", "pointer");
e.Row.Cells[0].Controls.Add(img);
e.Row.Cells[0].Style["font-weight"] = "bold";
}
else
{
indent += 16;
}
e.Row.Cells[0].Style["padding-left"] = indent + "px";
e.Row.Cells[0].Controls.Add(new LiteralControl(e.Row.Cells[0].Text));
# endregion
e.Row.Attributes["onmouseover"] = "style.backgroundColor='#F2FDDB'";
e.Row.Attributes["onmouseout"] = "style.backgroundColor=''";
}
}
}
}
用到的小图片: