本文主要是介绍如何将树与数据库邦定,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来源:愚翁专栏
以前写了一个例子,演示如何通过数据库数据动态加载TreeNode,参看:
Http://blog.csdn.net/knight94/archive/2006/03/24/637699.aspx
可能原先的例子,只是显示如何动态加载一个树,没有考虑到后期的修改或删除之类的操作,因此我在原有基础上,扩展了更新操作。不过实现的基础,还是在程序中保存一个临时数据表,要注意的是,如果数据量比较大的话,考虑到效率以及资源使用等因素,则可以取消临时数据表,而直接去访问数据库即可,这可以根据实际应用来修改我的方法。
以下就具体说说实现的步骤。
首先,是创建临时数据表。
private DataTable dtTree = null;
private void CreateTable()
{
// Create data table
dtTree = new DataTable( "TreeDBInfo" );
// Add new data columns
DataColumn dc = new DataColumn( "EmployeeID", typeof(int) );
dtTree.Columns.Add( dc );
// Set primary key column
dtTree.PrimaryKey = new DataColumn[]{dc};
dtTree.Columns.Add( new DataColumn( "EmployeeName", typeof(string) ) );
dtTree.Columns.Add( new DataColumn( "Age", typeof(int) ) );
dtTree.Columns.Add( new DataColumn( "Salary", typeof(double) ) );
dtTree.Columns.Add( new DataColumn( "Address", typeof(string) ) );
dtTree.Columns.Add( new DataColumn( "EmpParentID", typeof(int) ) );
// Add records into table
//'0' is that the type has no parent
dtTree.Rows.Add( new object[]{1, "Parent1",30, 2000,"***", 0} );
dtTree.Rows.Add( new object[]{2, "Parent2",30, 2000,"***", 0} );
dtTree.Rows.Add( new object[]{3, "Parent3",32, 2000,"***", 0} );
dtTree.Rows.Add( new object[]{4, "Child1",25, 1400,"***", 1} );
dtTree.Rows.Add( new object[]{5, "Child2",27, 1600,"***", 1} );
dtTree.Rows.Add( new object[]{6, "Child3",27, 1600,"***", 2} );
dtTree.Rows.Add( new object[]{7, "GrandChild1",23, 1200,"***", 4} );
dtTree.Rows.Add( new object[]{8, "GrandChild2",23, 1200,"***", 4} );
dtTree.Rows.Add( new object[]{9, "GrandChild3",22, 1200,"***", 5} );
}
接着就是通过数据表来加载TreeNode。
private void CreateTree()
{
DataRow[] drArray = dtTree.Select( "EmpParentID=0",
"EmpParentID ASC",
DataViewRowState.CurrentRows );
if( drArray.Length == 0 ) return;
TreeNode tnNew = null;
foreach( DataRow dr in drArray )
{
tnNew = trvDBBinding.Nodes.Add( dr["EmployeeName"].ToString() );
tnNew.Tag = dr["EmployeeID"].ToString();//Save "EmployeeID" in node's tag
CreateTreeNode( ref tnNew );
}
}
private void CreateTreeNode( ref TreeNode tnParent )
{
//Get children data info
DataRow[] drArray = dtTree.Select(
string.Format( "EmpParentID = {0}", tnParent.Tag ),
"EmpParentID ASC",
DataViewRowState.CurrentRows );
if( drArray.Length == 0 ) return;
TreeNode tnNew = null;
foreach( DataRow dr in drArray )
{
tnNew = tnParent.Nodes.Add( dr["EmployeeName"].ToString() );
tnNew.Tag = dr["EmployeeID"].ToString();//Save "EmployeeID" in node's tag
CreateTreeNode( ref tnNew );
}
}
这里需要注意的是,要保存每条记录的主键到每个TreeNode的Tag属性中,即使动态从数据库查询也是一样的,这样能唯一标明记录,从而快速定位。
为了方便操作,我用弹出对话框的方法来查看或者修改TreeNode信息,那么对话框的代码大致如下:
private DataTable dtTree = null;
private TreeNode tnSelected = null;
// frmViewNode constructor
public frmViewNode( ref DataTable TreeTable, ref TreeNode SelectedNode )
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
dtTree = TreeTable;
tnSelected = SelectedNode;
}
private void ShowNodeInfo()
{
// Find node info from data table
DataRow[] drArray = dtTree.Select(
string.Format( "EmployeeID = {0}", tnSelected.Tag ),
"EmployeeID ASC",
DataViewRowState.CurrentRows );
if( drArray.Length == 0 ) return;
else if( drArray.Length == 1 )
{
// Show tree node info
txtName.Text = drArray[0]["EmployeeName"].ToString();
txtAge.Text = drArray[0]["Age"].ToString();
txtSalary.Text = drArray[0]["Salary"].ToString();
txtAddress.Text = drArray[0]["Address"].ToString();
}
}
private void frmViewNode_Load(object sender, System.EventArgs e)
{
ShowNodeInfo();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
// Find node info from data table
DataRow[] drArray = dtTree.Select(
string.Format( "EmployeeID = {0}", tnSelected.Tag ),
"EmployeeID ASC",
DataViewRowState.CurrentRows );
if( drArray.Length == 0 ) return;
else if( drArray.Length == 1 )
{
// Update data to data table
drArray[0].BeginEdit();
drArray[0]["EmployeeName"] = txtName.Text;
drArray[0]["Age"] = txtAge.Text;
drArray[0]["Salary"] = txtSalary.Text;
drArray[0]["Address"] = txtAddress.Text;
drArray[0].EndEdit();
}
// Update tree node
tnSelected.Text = txtName.Text;
this.DialogResult = DialogResult.OK;
}
则在显示TreeView的窗体中弹出以上窗体的代码如下:
private TreeNode tnSelected = null;
private void trvDBBinding_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Left )
{
tnSelected = trvDBBinding.GetNodeAt( e.X, e.Y );
}
}
private void trvDBBinding_DoubleClick(object sender, System.EventArgs e)
{
if( tnSelected != null )
{
//Open window to view treenode info
frmViewNode myViewNode = new frmViewNode( ref dtTree, ref tnSelected );
if( myViewNode.ShowDialog() == DialogResult.OK )
{
//Update database using "dtTree"
dtTree.AcceptChanges();//Update datatable
}
}
}
private void trvDBBinding_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
tnSelected = e.Node;
}
要注意的是,如何去更新数据库,是在“ DoubleClick ”事件中去做还是在弹出窗体中去做。我个人推荐在弹出的窗体中去更新,因为效率比较高,不管是通过临时数据表还是动态从数据库进行加载。
如果使用临时数据表的话,因为在更新DataTable的时候,已经定位到具体记录了,那么只要用DataAdapter去执行Update方法即可(别忘了给DataAdapter设定UpdateCommand,否则数据库什么都不会更新);如果使用动态数据表的话,只需要设置相应的DBCommand,来进行更新操作即可。
这篇关于如何将树与数据库邦定的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!