怎么把如下数据库绑定到treeview中 在form环境数据库:id mc(名称)0001 打印纸 0002 剪刀0003 笔0004 笔芯0005 电池要实现treeView:样子 全部 ┝打印纸 ┝剪刀 ┝笔 ┝笔芯 ┕电池 点击打印纸可以传值“0001”过去 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 取数据到 _DataTable里 TreeNode _AllNode = new TreeNode("全部"); for (int i = 0; i != _DataTable.Rows.Count; i++) { TreeNode _Node = new TreeNode(); _Node.Text = _DataTable.Rows[i]["mc"].ToString(); _Node.tag = _DataTable.Rows[i]["id"].ToString(); _AllNode.Nodes.Add(_Node); } treeView1.Nodes.Add(_AllNode);获取数据的时候 treeView1.SelectedNode.Tag; /// <summary> /// Loads the tree view. /// </summary> /// <param name="oTreeView">The o tree view.</param> /// <param name="TitleText">The title text.</param> /// <param name="TextField">The text field.</param> /// <param name="TextFormatString">The text format string.</param> /// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param> /// <param name="NavigateUrl">The navigate URL.</param> /// <param name="Target">The target.</param> /// <param name="sql">The SQL.</param> public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql) { int i = 0, j = 0; Guid ParentID = Guid.Empty; if (SwitchLocale) { //TextField = GetSwitchLocale(TextField); } //TreeNode oTNode = new TreeNode(); //oTNode.Value = "LUAutumnAidStudy"; //oTNode.Text = TitleText; Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index TreeNode oNode; string[] orderBy = { "FullName" }; LUAutumnAidStudyCollection source; if (sql != string.Empty) { source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true); } else { source = LUAutumnAidStudy.LoadCollection(orderBy, true); } // loop throught the Class table to build the Nodes and the List TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy foreach (LUAutumnAidStudy item in source) { oNode = new TreeNode(); oNode.Value = item.LUAASId.ToString(); oNode.Text = GetFormatedText(item, TextField, TextFormatString); oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString()); oNode.Target = Target; oNode.SelectAction = TreeNodeSelectAction.Expand; oNodes[i] = oNode; tblNodes.Add(item.LUAASId.ToString(), i); ++i; } // loop throught the Class table again to put the Nodes into proper hirearchy position foreach (LUAutumnAidStudy item in source) { i = (int)tblNodes[item.LUAASId.ToString()]; ParentID = Guid.Empty; if (tblNodes[ParentID.ToString()] == null) { j = 0; } else { j = (int)tblNodes[ParentID.ToString()]; } if (ParentID != Guid.Empty) { oNodes[j].ChildNodes.Add(oNodes[i]); } } //loop throught the Class table again to put the Top Nodes into TreeView foreach (LUAutumnAidStudy item in source) { i = (int)tblNodes[item.LUAASId.ToString()]; ParentID = Guid.Empty; if (ParentID == Guid.Empty) { if (oNodes[i] != null) { oTreeView.Nodes.Add(oNodes[i]); } } } //oTreeView.Nodes.Add(oTNode);; } /// <summary> /// Loads the tree view. /// </summary> /// <param name="oTreeView">The o tree view.</param> /// <param name="TitleText">The title text.</param> /// <param name="TextField">The text field.</param> /// <param name="TextFormatString">The text format string.</param> /// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param> /// <param name="NavigateUrl">The navigate URL.</param> /// <param name="Target">The target.</param> /// <param name="sql">The SQL.</param> public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql) { int i = 0, j = 0; Guid ParentID = Guid.Empty; if (SwitchLocale) { //TextField = GetSwitchLocale(TextField); } //TreeNode oTNode = new TreeNode(); //oTNode.Value = "LUAutumnAidStudy"; //oTNode.Text = TitleText; Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index TreeNode oNode; string[] orderBy = { "FullName" }; LUAutumnAidStudyCollection source; if (sql != string.Empty) { source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true); } else { source = LUAutumnAidStudy.LoadCollection(orderBy, true); } // loop throught the Class table to build the Nodes and the List TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy foreach (LUAutumnAidStudy item in source) { oNode = new TreeNode(); oNode.Value = item.LUAASId.ToString(); oNode.Text = GetFormatedText(item, TextField, TextFormatString); oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString()); oNode.Target = Target; oNode.SelectAction = TreeNodeSelectAction.Expand; oNodes[i] = oNode; tblNodes.Add(item.LUAASId.ToString(), i); ++i; } // loop throught the Class table again to put the Nodes into proper hirearchy position foreach (LUAutumnAidStudy item in source) { i = (int)tblNodes[item.LUAASId.ToString()]; ParentID = Guid.Empty; if (tblNodes[ParentID.ToString()] == null) { j = 0; } else { j = (int)tblNodes[ParentID.ToString()]; } if (ParentID != Guid.Empty) { oNodes[j].ChildNodes.Add(oNodes[i]); } } //loop throught the Class table again to put the Top Nodes into TreeView foreach (LUAutumnAidStudy item in source) { i = (int)tblNodes[item.LUAASId.ToString()]; ParentID = Guid.Empty; if (ParentID == Guid.Empty) { if (oNodes[i] != null) { oTreeView.Nodes.Add(oNodes[i]); } } } //oTreeView.Nodes.Add(oTNode);; } /// <summary> /// Gets the formated text. /// </summary> /// <param name="target">The target.</param> /// <param name="textField">The text field.</param> /// <param name="textFormatString">The text format string.</param> /// <returns></returns> private static string GetFormatedText(LUAutumnAidStudy target, string[] textField, string textFormatString) { for (int i = 0; i < textField.Length; i++) { PropertyInfo pi = target.GetType().GetProperty(textField[i]); textFormatString = textFormatString.Replace("{" + i.ToString() + "}", (string)pi.GetValue(target, null)); } return textFormatString; } 将数据取出来,然后生成树节点。 //添加树的父节点 private void AddParent(string str) { TreeNode tmp; tmp = new TreeNode(); tmp.ImageIndex = 0; tmp.SelectedImageIndex = 0; tmp.Text = str; this.treeView_logdate.Nodes.Add(tmp); JoinParameter.tNFisrt = tmp; } //添加树的子节点 private void AddChildNode(TreeNode parentTN, string str) { TreeNode tmp; tmp = new TreeNode(); tmp.ImageIndex = 2; tmp.SelectedImageIndex = 2; tmp.Text = str;// tmp.tag=对象 这里可以将一个对象绑定到某一个树节点下,取值的时候再强制转化一样//就可以得到一个对象 parentTN.Nodes.Add(tmp); } //联系几个参数的类 public class JoinParameter { public static TreeNode tNFisrt = null; public static TreeNode tNSecond = null; } 复杂点的The server tag is not well formed.问题 serialPort显示数据问题 100分求一个正则表达式的写法 用多线程展开子节点 求救 急 请问怎么样写一个Split的逆方法 monthcalendar与combobox配合使用时的问题 C#中怎么访问修改另外一个窗口的变量? 放弃C++投入C++++的怀抱~~~!!!!?????? 不能包含某些字符的正则表达式如何写。 如何得到本机的工作组或域的名称???help aspx页面清空HTML 字符串如何进行换位
TreeNode _AllNode = new TreeNode("全部"); for (int i = 0; i != _DataTable.Rows.Count; i++)
{
TreeNode _Node = new TreeNode();
_Node.Text = _DataTable.Rows[i]["mc"].ToString();
_Node.tag = _DataTable.Rows[i]["id"].ToString(); _AllNode.Nodes.Add(_Node);
} treeView1.Nodes.Add(_AllNode);获取数据的时候 treeView1.SelectedNode.Tag;
/// Loads the tree view.
/// </summary>
/// <param name="oTreeView">The o tree view.</param>
/// <param name="TitleText">The title text.</param>
/// <param name="TextField">The text field.</param>
/// <param name="TextFormatString">The text format string.</param>
/// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param>
/// <param name="NavigateUrl">The navigate URL.</param>
/// <param name="Target">The target.</param>
/// <param name="sql">The SQL.</param>
public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql)
{
int i = 0, j = 0;
Guid ParentID = Guid.Empty; if (SwitchLocale)
{
//TextField = GetSwitchLocale(TextField);
}
//TreeNode oTNode = new TreeNode();
//oTNode.Value = "LUAutumnAidStudy";
//oTNode.Text = TitleText; Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index
TreeNode oNode;
string[] orderBy = { "FullName" };
LUAutumnAidStudyCollection source;
if (sql != string.Empty)
{
source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true);
}
else
{
source = LUAutumnAidStudy.LoadCollection(orderBy, true);
} // loop throught the Class table to build the Nodes and the List
TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy
foreach (LUAutumnAidStudy item in source)
{
oNode = new TreeNode();
oNode.Value = item.LUAASId.ToString();
oNode.Text = GetFormatedText(item, TextField, TextFormatString);
oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString());
oNode.Target = Target;
oNode.SelectAction = TreeNodeSelectAction.Expand;
oNodes[i] = oNode;
tblNodes.Add(item.LUAASId.ToString(), i);
++i;
} // loop throught the Class table again to put the Nodes into proper hirearchy position
foreach (LUAutumnAidStudy item in source)
{
i = (int)tblNodes[item.LUAASId.ToString()];
ParentID = Guid.Empty; if (tblNodes[ParentID.ToString()] == null)
{
j = 0;
}
else
{
j = (int)tblNodes[ParentID.ToString()];
} if (ParentID != Guid.Empty)
{
oNodes[j].ChildNodes.Add(oNodes[i]);
}
} //loop throught the Class table again to put the Top Nodes into TreeView
foreach (LUAutumnAidStudy item in source)
{
i = (int)tblNodes[item.LUAASId.ToString()];
ParentID = Guid.Empty; if (ParentID == Guid.Empty)
{
if (oNodes[i] != null)
{
oTreeView.Nodes.Add(oNodes[i]);
}
}
}
//oTreeView.Nodes.Add(oTNode);;
}
/// <summary>
/// Loads the tree view.
/// </summary>
/// <param name="oTreeView">The o tree view.</param>
/// <param name="TitleText">The title text.</param>
/// <param name="TextField">The text field.</param>
/// <param name="TextFormatString">The text format string.</param>
/// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param>
/// <param name="NavigateUrl">The navigate URL.</param>
/// <param name="Target">The target.</param>
/// <param name="sql">The SQL.</param>
public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql)
{
int i = 0, j = 0;
Guid ParentID = Guid.Empty; if (SwitchLocale)
{
//TextField = GetSwitchLocale(TextField);
}
//TreeNode oTNode = new TreeNode();
//oTNode.Value = "LUAutumnAidStudy";
//oTNode.Text = TitleText; Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index
TreeNode oNode;
string[] orderBy = { "FullName" };
LUAutumnAidStudyCollection source;
if (sql != string.Empty)
{
source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true);
}
else
{
source = LUAutumnAidStudy.LoadCollection(orderBy, true);
} // loop throught the Class table to build the Nodes and the List
TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy
foreach (LUAutumnAidStudy item in source)
{
oNode = new TreeNode();
oNode.Value = item.LUAASId.ToString();
oNode.Text = GetFormatedText(item, TextField, TextFormatString);
oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString());
oNode.Target = Target;
oNode.SelectAction = TreeNodeSelectAction.Expand;
oNodes[i] = oNode;
tblNodes.Add(item.LUAASId.ToString(), i);
++i;
} // loop throught the Class table again to put the Nodes into proper hirearchy position
foreach (LUAutumnAidStudy item in source)
{
i = (int)tblNodes[item.LUAASId.ToString()];
ParentID = Guid.Empty; if (tblNodes[ParentID.ToString()] == null)
{
j = 0;
}
else
{
j = (int)tblNodes[ParentID.ToString()];
} if (ParentID != Guid.Empty)
{
oNodes[j].ChildNodes.Add(oNodes[i]);
}
} //loop throught the Class table again to put the Top Nodes into TreeView
foreach (LUAutumnAidStudy item in source)
{
i = (int)tblNodes[item.LUAASId.ToString()];
ParentID = Guid.Empty; if (ParentID == Guid.Empty)
{
if (oNodes[i] != null)
{
oTreeView.Nodes.Add(oNodes[i]);
}
}
}
//oTreeView.Nodes.Add(oTNode);;
}
/// <summary>
/// Gets the formated text.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="textField">The text field.</param>
/// <param name="textFormatString">The text format string.</param>
/// <returns></returns>
private static string GetFormatedText(LUAutumnAidStudy target, string[] textField, string textFormatString)
{
for (int i = 0; i < textField.Length; i++)
{
PropertyInfo pi = target.GetType().GetProperty(textField[i]);
textFormatString = textFormatString.Replace("{" + i.ToString() + "}", (string)pi.GetValue(target, null));
}
return textFormatString;
}
//添加树的父节点
private void AddParent(string str)
{
TreeNode tmp;
tmp = new TreeNode();
tmp.ImageIndex = 0;
tmp.SelectedImageIndex = 0;
tmp.Text = str;
this.treeView_logdate.Nodes.Add(tmp);
JoinParameter.tNFisrt = tmp;
}
//添加树的子节点
private void AddChildNode(TreeNode parentTN, string str)
{
TreeNode tmp;
tmp = new TreeNode();
tmp.ImageIndex = 2;
tmp.SelectedImageIndex = 2;
tmp.Text = str;
// tmp.tag=对象 这里可以将一个对象绑定到某一个树节点下,取值的时候再强制转化一样
//就可以得到一个对象
parentTN.Nodes.Add(tmp); }
//联系几个参数的类
public class JoinParameter
{
public static TreeNode tNFisrt = null;
public static TreeNode tNSecond = null;
}