支持樹型的GridView
實現思路
源代碼下載
效果圖
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
[ToolboxData(
public class TreeListView : GridView
{
public TreeListView()
{
base
base
base
}
Tree的屬性設置#region Tree的屬性設置
private int _nodeColumnIndex;
/**//// <summary>
/// 顯示樹型的列 Index
/// </summary>
public int NodeColumnIndex
{
get { return this
set
{
_nodeColumnIndex = value;
}
}
private string _columnKey;
/**//// <summary>
/// Key字段
/// </summary>
public string ColumnKey
{
get { return _columnKey; }
set
{
_columnKey = value;
}
}
private string _parentKey;
/**//// <summary>
/// 指向父節點的字段
/// </summary>
public string ParentKey
{
set
{
_parentKey = value;
}
}
private string _sortKey;
/**//// <summary>
/// 排序字段
/// </summary>
public string SortKey
{
set { _sortKey = value; }
}
private object _rootNodeFlag;
/**//// <summary>
/// 根節點的標記 這裡采用 ParentKey為什麼字符
/// </summary>
public object RootNodeFlag
{
set
{
_rootNodeFlag = value;
}
}
private static string _treeImageFolder =
public static string TreeImageFolder
{
get
{
return _treeImageFolder;
}
set
{
_treeImageFolder = value;
}
}
private int _expendDepth =
/**//// <summary>
/// 展開的深度
/// </summary>
public int ExpendDepth
{
get
{
return _expendDepth;
}
set { _expendDepth = value; }
}
#endregion
public override object DataSource
{
get
{
return base
}
set
{
DataTable dtSource = new DataTable();
if (value is DataSet && ((DataSet)value)
{
DataSet ds = value as DataSet;
dtSource = OrderData(ds
}
else
{
throw new Exception(
}
base
}
}
DataTable OrderData(DataTable dtSource)
{
DataTable dtResult = dtSource
dtResult
dtResult
dtResult
dtResult
dtResult
RecursionOrderData(dtSource
return dtResult;
}
string FormatToRowFilter(object val)
{
Type type = val
if (type == typeof(string))
{
return string
}
else if (type == typeof(Guid))
{
return string
}
else if (type
{
return val
}
else
{
return string
}
}
bool RecursionOrderData(DataTable dtSource
{
DataView dv = new DataView(dtSource);
dv
dv
DataRow dr = null;
depth++;
for (int i =
{
dr = dtResult
for (int j =
{
dr[j] = dv[i][j];
}
if (i == dv
{
dr[
}
else
{
dr[
}
dr[
dr[
if (depth ==
{
dr[
}
else
{
dr[
}
dtResult
dr[
}
return dv
}
public override bool AllowPaging
{
get
{
return base
}
set
{
base
}
}
public override bool AutoGenerateColumns
{
get
{
return base
}
set
{
base
}
}
重載:CreateRow#region 重載:CreateRow
protected override GridViewRow CreateRow(int rowIndex
{
return new TreeListViewRow(rowIndex
}
#endregion
重寫:Rows#region 重寫:Rows
private TreeListViewRowCollection _rowsCollection;
[Browsable(false)]
public new TreeListViewRowCollection Rows
{
get
{
ArrayList _rowsArray = new ArrayList();
for (int i =
{
_rowsArray
}
this
return this
}
}
#endregion
重載:OnInit#region 重載:OnInit
protected override void OnInit(EventArgs e)
{
base
Page
if (!Page
{
this
}
}
#endregion
IPostBackDataHandler Members#region IPostBackDataHandler Members
public bool LoadPostData(string postDataKey
{
return false;
}
public void RaisePostDataChangedEvent()
{
}
#endregion
方法:RenderCheckBoxExField#region 方法:RenderCheckBoxExField
/**//// <summary>
/// 處理CheckBoxExField類型的列
/// </summary>
private void RenderCheckBoxExField()
{
if (!this
{
return;
}
foreach (DataControlField field in Columns)
{
if (field is CheckBoxExField)
{
int checkBoxExFieldIndex = Columns
foreach (GridViewRow row in Rows)
{
if (row
{
row
}
if (row
{
row
((CheckBox)row
}
}
注冊腳本#region 注冊腳本
string script = @
var modifyId =
var choosedId =
var choosedIndex;
function ChooseTree(obj)
{
var cTrId = obj
var treeTable = document
for( var i =
{
if( treeTable
{
document
}
}
choosedId =
choosedIndex = new Array();
for( var i =
{
if( document
{
choosedId += treeTable
choosedIndex
}
}
choosedId = choosedId
}
if (!Page
{
Page
}
#endregion
}
}
}
方法:GetAutoGenerateButtonCount#region 方法:GetAutoGenerateButtonCount
private int GetAutoGenerateButtonCount()
{
int num =
if (this
{
num =
}
return num;
}
#endregion
#endregion
protected override void Render(HtmlTextWriter writer)
{
RenderCheckBoxExField();
base
}
}
public class TreeListViewRow : GridViewRow
{
public TreeListViewRow(int rowIndex
: base(rowIndex
{
}
protected override void OnPreRender(EventArgs e)
{
base
if (this
{
if (this
{
TreeListView treeListView = this
DataRow dr = ((DataTable)treeListView
string str = GetTreeNodeImg(dr
this
this
if (treeListView
{
this
}
}
}
}
獲取Tree的圖片#region 獲取Tree的圖片
string GetTreeNodeImg(DataRow dr
{
return GetTreeNodeOtherImg(dr) + GetTreeNodeLastImg(isLeaf
}
string GetTreeNodeOtherImg(DataRow dr)
{
if (dr[
{
DataRow drParentRow = dr[
bool parentIsBottom = Convert
if (parentIsBottom)
{
return GetTreeNodeOtherImg(drParentRow) + string
}
else
{
return GetTreeNodeOtherImg(drParentRow) + string
}
}
else
{
return string
}
}
string GetTreeNodeLastImg(bool isLeaf
{
//最後靠近的那個Image
string lastImg = string
if (isLeaf)
{
if (isBottom)
{
lastImg = string
}
else
{
lastImg = string
}
}
else
{
if (isBottom)
{
lastImg = string
}
else
{
lastImg = string
}
}
return lastImg;
}
#endregion
Js代碼實現折疊效果
Code
var lExpend =
var lPinch =
var tExpend =
var tPinch =
function ClickNode(img
{
var imgId = img
var url = img
var oldTrId = img
var newTrId = oldTrId
if( isBottom)
{
if( imgId == lExpend)
{
img
img
PinchNode(newTrId
}
else
{
img
img
ExpendNode(newTrId
}
}
else
{
if( imgId == tExpend )
{
img
img
PinchNode(newTrId
}
else
{
img
img
ExpendNode(newTrId
}
}
}
function ExpendNode(newId
{
var tree = document
for( var i =
{
if( tree
{
var isExpend = true;
var pId = tree
while( pId != oldId)
{
for( var j =
pId = pId
var parent = document
if( parent != null )
{
var tempId = parent
if( tempId == lExpend || tempId == tExpend || tempId ==
;
else
{
isExpend = false;
break;
}
}
else
break;
}
if( isExpend )
tree
}
}
}
function PinchNode(newId
{
var tree = document
for( var i =
{
if( tree
{
tree
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12471.html