请教帮忙看下哪错了,谢谢!
这是后台代码:namespace ga._62.web.sys.aspx
{
public partial class LI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createTreeView();
}
}
private void createTreeView()
{
const int virtualRootID = 0;//虚拟的根结点为0;
Department[] dep = GetDepartment();
createTreeViewRecursive(TreeView1.Nodes, dep, 0);
}
protected Department[] GetDepartment()
{
return new Department().LoadAll();
}
private void createTreeViewRecursive(TreeNodeCollection nodes, DataTable datasource, int PID)
{
string filter = string.Format("PID={0};PID");
DataRow[] drArr = datasource.Select(filter);
TreeNode node;
foreach (DataRow dr in drArr)
{
node = new TreeNode();
nodes.Add(node);
node.Text = (string)dr["Name"];
createTreeViewRecursive(node.ChildNodes, datasource, (int)dr["ID"]);
datasource.Rows.Remove(dr);
}
}
}
}
这下面是数据访问层的代码:
protected virtual T[] LoadMore(string strSql)
{
IDataReader dr = null;
T[] rdo = null;
try
{
int count = mdbHelper.ExecuteResultCount(strSql);
if (count < 1)
return null;
rdo = new T[count];
dr = mdbHelper.ExecuteReader(strSql);
LoadToArray(rdo, dr);
return rdo;
}
finally
{
mdbHelper.ReleaseDataReader(dr);
}
}
private T[] LoadMore(string strSql, string strOrderFields, int currPage, int rowsPerPage)
{
strSql = mdbHelper.GetPageSql(strSql, strOrderFields, currPage, rowsPerPage);
return LoadMore(strSql);
}
/// <summary>
/// 检索所有记录
/// </summary>
public T[] LoadAll()
{
return LoadMore(Sql.GetSelectAll());
}
public T[] LoadAll(string strOrderFields, int currPage, int rowsPerPage)
{
return LoadMore(Sql.GetSelectAll(), strOrderFields, currPage, rowsPerPage);
}
public T[] LoadAll(int currPage, int rowsPerPage)
{
return LoadAll("ID", currPage, rowsPerPage);
}
/// <summary>
/// 检索指定条件的所有记录
/// 检索条件字段名(类的数据成员),
/// 字段值通过反射由SQLPlus类获取
/// 使用 = 作为比较符号,如 UserName = 'guest'
/// </summary>
public T[] LoadAll(string strFieldName)
{
return LoadMore(Sql.GetSelect(null, strFieldName));
}
/// <summary>
/// 检索指定条件的所有记录
/// String strFieldNames 检索条件字段名列表(类的数据成员),
/// 字段值通过反射由SQLPlus类获取
/// 使用 = 作为比较符号,如 UserName = 'guest' and IsAmdin = 1
/// 检索到的所有DataObject
/// </summary>
public T[] LoadAll(string [] strFieldNames)
{
return LoadMore( Sql.GetSelect(null, strFieldNames));
}
/// <summary>
/// 按照传入的SQL脚本检索数据
/// String strSQL,传入的SQL脚本
/// 检索到的所有DataObject
/// </summary>
public T[] LoadBySql(string strSql)
{
return LoadMore(strSql);
}
public T[] LoadBySql(string strSql, string strOrderFields, int currPage, int rowsPerPage)
{
return LoadMore(strSql, strOrderFields, currPage, rowsPerPage);
}
public T[] LoadBySql(string strSql, int currPage, int rowsPerPage)
{
return LoadBySql(strSql, "ID", currPage, rowsPerPage);
}
为什么我这里用不了?我用的是treeview,想显示数据库里的东西,用递归的方法,错了没有?为什么会运行不了,提示我的是:那个数组不能转换为datatable
怎么办啊?请各位高手帮忙看下,谢谢! --------------------编程问答-------------------- 太长了,懒的细看。建议你单步跟踪调试,并贴出错误消息。 --------------------编程问答-------------------- createTreeViewRecursive(TreeView1.Nodes, dep, 0); --------------------编程问答-------------------- createTreeViewRecursive(TreeView1.Nodes, dep, 0);
这个地方错了,怎么办啊? --------------------编程问答-------------------- 请教怎么办啊?
给50分!
--------------------编程问答-------------------- 应该是在递归构造Tree的时候出的问题。
给你一个递归构造TreeView的例子。参考一下改吧。
--------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- up --------------------编程问答-------------------- 楼上的哥哥,你看下我的代码,我用的递归没错啊,就是提示说我的那个name只能存一个字符,是怎么回事啊?请教了,谢谢!
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.InitTree();
}
private DataView GetData()
{
DataTable table = new DataTable();
table.Columns.Add("CID", typeof(int));
table.Columns.Add("CName", typeof(string));
table.Columns.Add("ParentID", typeof(int));
DataRow row0 = table.NewRow();
DataRow row1 = table.NewRow();
DataRow row2 = table.NewRow();
DataRow row3 = table.NewRow();
DataRow row4 = table.NewRow();
DataRow row5 = table.NewRow();
DataRow row6 = table.NewRow();
DataRow row7 = table.NewRow();
DataRow row8 = table.NewRow();
DataRow row9 = table.NewRow();
row0.ItemArray = new object[] { 1, ".net", 0 };
row1.ItemArray = new object[] { 2, "c#", 1 };
row2.ItemArray = new object[] { 3, "asp.net", 1 };
row3.ItemArray = new object[] { 4, "office", 0 };
row4.ItemArray = new object[] { 5, "word", 4 };
row5.ItemArray = new object[] { 6, "excel", 5 };
row6.ItemArray = new object[] { 7, "os", 0 };
row7.ItemArray = new object[] { 8, "unix", 7 };
row8.ItemArray = new object[] { 9, "linux", 8 };
row9.ItemArray = new object[] { 10, "c#.net", 2 };
table.Rows.Add(row0);
table.Rows.Add(row1);
table.Rows.Add(row2);
table.Rows.Add(row3);
table.Rows.Add(row4);
table.Rows.Add(row5);
table.Rows.Add(row6);
table.Rows.Add(row7);
table.Rows.Add(row8);
table.Rows.Add(row9);
return table.DefaultView;
}
public void InitTree()
{
DataView dv = this.GetData();
dv.RowFilter = "ParentID=0";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["cname"].ToString();
node.Value = drv["cid"].ToString();
node.Expanded = true;
this.TreeView1.Nodes.Add(node);
BuildTree(dv, node);
}
}
private void BuildTree(DataView dv, TreeNode pnode)
{
dv.RowFilter = "[ParentID]=" + pnode.Value;
foreach (DataRowView drv in dv)
{
string name = drv["cname"].ToString();
TreeNode cnode = new TreeNode(name);
cnode.Value = drv["cid"].ToString();
if (cnode.Value != pnode.Value)
{
pnode.ChildNodes.Add(cnode);
BuildTree(dv, cnode);
}
}
}
}
}
public partial class LI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createTreeView();
}
}
private void createTreeView()
{
const int virtualRootID = 0;//虚拟的根结点为0;
Department[] dep = GetDepartment();
Department depar = new Department();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("ID",typeof(Int32));
dt.Columns.Add(dc);
dc = new DataColumn("Name", typeof(String));
dt.Columns.Add(dc);
dc = new DataColumn("OrganizationID", typeof(Int32));
dt.Columns.Add(dc);
dc = new DataColumn("LastLayerFlag", typeof(Boolean));
dt.Columns.Add(dc);
dc = new DataColumn("LayerCode", typeof(String));
dt.Columns.Add(dc);
dc = new DataColumn("PID", typeof(Int32));
dt.Columns.Add(dc);
DataRow dr=dt.NewRow();
dr["ID"] = depar.ID;
dr["Name"] = dep[10].Name;
dr["OrganizationID"] =dep[10].OrganizationID;
dr["LastLayerFlag"] = dep[1].LastLayerFlag;
dr["LayerCode"] = dep[10].LayerCode;
dr["PID"] =dep[10].PID;
dt.Rows.Add(dr);
createTreeViewRecursive(TreeView1.Nodes, dt, 0);
}
protected Department[] GetDepartment()
{
return new Department().LoadAll();
}
private void createTreeViewRecursive(TreeNodeCollection nodes, DataTable datasource, int PID)
{
string filter = string.Format("PID={0};PID");
DataRow[] drArr = datasource.Select(filter);
TreeNode node;
foreach (DataRow dr in drArr)
{
node = new TreeNode();
nodes.Add(node);
node.Text = (string)dr["Name"];
createTreeViewRecursive(node.ChildNodes, datasource, (int)dr["ID"]);
datasource.Rows.Remove(dr);
}
}
}
} --------------------编程问答-------------------- ?????????????? --------------------编程问答-------------------- 怎么没人帮我想想啊?谢谢啊!
补充:.NET技术 , ASP.NET