/// <summary>
/// TreeView控件绑定到数据源
/// </summary>
/// <param name="tv">TreeView控件</param>
/// <param name="img">ImageList控件</param>
/// <param name="rootName">根节点的文本属性值</param>
/// <param name="table">要绑定的数据表</param>
/// <param name="Code">数据表的代码列</param>
/// <param name="Name">数据表的名称列</param>
public void BaindTreeView(TreeView tv,ImageList img,string rootName, string table, string Code, string Name)
{
string strSql = null;
DataSet ds = null;
DataTable dt = null;
TreeNode rootNode = null;
TreeNode childNode = null;
strSql = "select "+Code+" , "+Name+" from "+table;
tv.Nodes.Clear();
tv.ImageList = img;
//创建根节点
rootNode = new TreeNode();
rootNode.Tag = null;
rootNode.Text = rootName;
rootNode.ImageIndex = 1;
rootNode.SelectedImageIndex = 0;
try
{
ds = db.GetDataSet(strSql, table); //
dt = ds.Tables[table];
foreach (DataRow row in dt.Rows)
{
//创建子节点
childNode = new TreeNode();
childNode.Tag = row[Code];
childNode.Text = row[Name].ToString();
childNode.ImageIndex = 1;
childNode.SelectedImageIndex = 0;
rootNode.Nodes.Add(childNode);
}
tv.Nodes.Add(rootNode);
tv.ExpandAll();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "软件提示");
throw e;
}
}