关于树形控件

最近一段时间一直在围绕树形控件在做东西
在最开始的一段时间里由于开始所用的数据类型是用数据组实现的
造成在后期的树形控件的节点的添加困难
最终还是取消了用数组来实现,而用了DataSet
主要相关代码:
private   void  treeView1_AfterSelect( object  sender, System.Windows.Forms.TreeViewEventArgs e)
        
{
            
try
            
{
                
//节点为根节点
                if(this.treeView1.SelectedNode.Parent ==null)
                
{
                    flagIOServer 
=true;
                    
this.tabControl1.SelectedIndex =0;
                    
this.menuItem_AddIOServer.Enabled=true;
                    
this.menuItem_AddSubSystem.Enabled=false;
                    
this.menuItem_AddSystem.Enabled=false;
                    
this.menuItem_AddTag.Enabled=false;
                    
this.menuItem_DeleteSubSystem.Enabled=false;
                    
this.menuItem_DeleteSystem.Enabled=false;
                    
this.menuItem_UpdateTag.Enabled=false;
                    ds.Tables.Clear();
                    comm.CommandText 
="select TagID 列表ID ,AutoSubSysID 子系统ID,TagCode 节点编码,TagName 节点名称,TagType 节点类型,TagValueType 数据类型,TagMaxValue 最大值,TagMinValue 最小值,TagAlartMax 报警最大值 ,TagAlartMin 报警最小值,TagSaveInterval 存储时间间隔,TagSaveIgBand 存储变化盲区,CreateTime 创建时间,TimeStampMode 时戳确定方式,TagAlartPos 报警点,TagAlartPosDes 报警点描述,Description 描述 from TagInfo";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"TagInfo");
                        conn.Close();
                        takeHideValue(
"TagInfo");
                        
this.dataGrid1.DataSource =ds.Tables["TagInfo"];
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"TagInfo");
                        conn.Close();
                        takeHideValue(
"TagInfo");
                        
this.dataGrid1.DataSource=ds.Tables["TagInfo"];
                    }

                }

                    
//节点为IOServer节点
                else if(this.treeView1.SelectedNode.Parent.Parent ==null)
                
{
                    flagSystem 
=true;
                    flagIOServer 
=false;
                    selectNode 
=this.treeView1.SelectedNode;
                    
this.tabControl1.SelectedIndex =2;
                    
this.menuItem_AddIOServer.Enabled=true;
                    
this.menuItem_AddSubSystem.Enabled=false;
                    
this.menuItem_AddSystem.Enabled=true;
                    
this.menuItem_AddTag.Enabled=false;
                    
this.menuItem_DeleteSubSystem.Enabled=false;
                    
this.menuItem_DeleteSystem.Enabled=false;
                    
this.menuItem_UpdateTag.Enabled=false;
                    
string ID =this.treeView1.SelectedNode.Text.ToString().Trim().Substring(0,10);
                    comm.CommandText
="select * from IOServerInfo where IOServerID =";
                    comm.CommandText
+="'"+ID+"'";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"tab_IO");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"tab_IO");
                        conn.Close();
                    }

                    
foreach(DataRow RowIO in ds.Tables["tab_IO"].Rows)
                    
{
                        
this.txt_IOServerCode.Text =RowIO[1].ToString().Trim();
                        
this.txt_IOServerName.Text=RowIO[2].ToString().Trim();
                        
this.rtxt_IOServer.Text=RowIO[4].ToString().Trim();
                    }

                    ds.Tables.Clear();
                    comm.CommandText
="select TagID 列表ID ,TagInfo.AutoSubSysID 子系统ID,TagCode 节点编码,TagName 节点名称,TagType 节点类型,TagValueType 数据类型 ,TagMaxValue 最大值,TagMinValue 最小值,TagAlartMax 报警最大值 ,TagAlartMin 报警最小值,TagSaveInterval 存储时间间隔,TagSaveIgBand 存储变化盲区,TagInfo.CreateTime 创建时间,TagInfo.TimeStampMode 时戳确定方式,TagAlartPos 报警点,TagAlartPosDes 报警点描述,TagInfo.Description 描述 from TagInfo where AutoSubSysID in (select AutoSubSysID from ";
                    comm.CommandText
+="AutoSubSystemInfo where AutoSysID in (select AutoSysID from AutoSystemInfo where IOServerID";
                    comm.CommandText
+=" in(select IOServerID from IOServerInfo where IOServerID =";
                    comm.CommandText
+="'"+ID+"'";
                    comm.CommandText
+=")))";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"Tag_IOServer");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"Tag_IOServer");
                        conn.Close();
                    }

                    takeHideValue(
"Tag_IOServer");
                    
this.dataGrid1.DataSource=ds.Tables["Tag_IOServer"];
                }

                    
//节点为系统节点
                else if(this.treeView1.SelectedNode.Parent.Parent.Parent ==null)
                
{
                    flagSysSub 
=true;
                    flagSystem 
=false;
                    selectNode 
=this.treeView1.SelectedNode;
                    
this.tabControl1.SelectedIndex =1;
                    
this.menuItem_AddIOServer.Enabled=false;
                    
this.menuItem_AddSubSystem.Enabled=true;
                    
this.menuItem_AddSystem.Enabled=true;
                    
this.menuItem_AddTag.Enabled=false;
                    
this.menuItem_DeleteSubSystem.Enabled=false;
                    
this.menuItem_DeleteSystem.Enabled=true;
                    
this.menuItem_UpdateTag.Enabled=false;
                    
string ID =this.treeView1.SelectedNode.Text.ToString().Trim().Substring(0,10);
                    comm.CommandText
="select * from AutoSystemInfo where AutoSysID =";
                    comm.CommandText
+="'"+ID+"'";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"tab_Sys");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"tab_Sys");
                        conn.Close();
                    }

                    
foreach(DataRow RowSys in ds.Tables["tab_Sys"].Rows)
                    
{
                        
this.txt_SysName.Text =RowSys[4].ToString().Trim();
                        
this.txt_SysCode.Text =RowSys[3].ToString().Trim();
                        
switch(RowSys[7].ToString())
                        
{
                            
case "0":this.cmb_SysMode.Text="实时";
                                
break;
                            
case "1":this.cmb_SysMode.Text="非实时";
                                
break;
                            
default:
                                
break;
                        }

                    }

                    
string IOID =this.treeView1.SelectedNode.Parent.Text.ToString().Trim().Substring(0,10);
                    comm.CommandText
="select * from IOServerInfo where IOServerID =";
                    comm.CommandText
+="'"+IOID+"'";
                    da 
=new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"SysIOID");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"SysIOID");
                        conn.Close();
                    }

                    
foreach(DataRow RowSysID in ds.Tables["SysIOID"].Rows)
                    
{
                        
this.txt_SysIOID.Text =IOID;
                        
this.txt_SysIOName.Text =RowSysID[2].ToString();
                    }

                    ds.Tables.Clear();
                    comm.CommandText
="select TagID 列表ID ,TagInfo.AutoSubSysID 子系统ID,TagCode 节点编码,TagName 节点名称,TagType 节点类型,TagValueType 数据类型 ,TagMaxValue 最大值,TagMinValue 最小值,TagAlartMax 报警最大值 ,TagAlartMin 报警最小值,TagSaveInterval 存储时间间隔,TagSaveIgBand 存储变化盲区,TagInfo.CreateTime 创建时间,TagInfo.TimeStampMode 时戳确定方式,TagAlartPos 报警点,TagAlartPosDes 报警点描述,TagInfo.Description 描述 from TagInfo where AutoSubSysID in (select AutoSubSysID from"
                    comm.CommandText
+=" AutoSubSystemInfo where AutoSysID in (select AutoSysID from AutoSystemInfo";
                    comm.CommandText
+=" where AutoSysID =";
                    comm.CommandText
+="'"+ID+"'";
                    comm.CommandText
+="))";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"Tag_Sys");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"Tag_Sys");
                        conn.Close();
                    }

                    takeHideValue(
"Tag_Sys");
                    
this.dataGrid1.DataSource=ds.Tables["Tag_Sys"];
                }

                    
//节点为子系统节点
                else if(this.treeView1.SelectedNode.Parent.Parent.Parent.Parent ==null)
                
{
                    flagSysSub 
=false;
                    selectNode 
=this.treeView1.SelectedNode;
                    
this.tabControl1.SelectedIndex =3;
                    
string ID = this.treeView1.SelectedNode.Text.ToString().Trim().Substring(0,10);
                    comm.CommandText
="select count(*) from TagInfo where AutoSubSysID =";
                    comm.CommandText
+="'"+ID+"'";
                    
int count;
                    
if(conn.State.ToString()=="Open")
                    
{
                        count
=Convert.ToInt32(comm.ExecuteScalar());
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        count 
=Convert.ToInt32(comm.ExecuteScalar());
                        conn.Close();
                    }

                    
if(count>0)
                    
{
                        
this.menuItem_AddIOServer.Enabled=false;
                        
this.menuItem_AddSubSystem.Enabled=true;
                        
this.menuItem_AddSystem.Enabled=false;
                        
this.menuItem_AddTag.Enabled=false;
                        
this.menuItem_DeleteSubSystem.Enabled=true;
                        
this.menuItem_DeleteSystem.Enabled=false;
                        
this.menuItem_UpdateTag.Enabled=true;
                    }

                    
else
                    
{
                        
this.menuItem_AddIOServer.Enabled=false;
                        
this.menuItem_AddSubSystem.Enabled=true;
                        
this.menuItem_AddSystem.Enabled=false;
                        
this.menuItem_AddTag.Enabled=true;
                        
this.menuItem_DeleteSubSystem.Enabled=true;
                        
this.menuItem_DeleteSystem.Enabled=false;
                        
this.menuItem_UpdateTag.Enabled=false;
                    }

                    comm.CommandText 
="select * from AutoSubSystemInfo where AutoSubSysID =";
                    comm.CommandText
+="'"+ID+"'";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"SubSys");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"SubSys");
                        conn.Close();
                    }

                    
foreach(DataRow RowSubSys in ds.Tables["Subsys"].Rows)
                    
{
                        
this.txt_SubCode.Text =RowSubSys[2].ToString();
                        
this.txt_SubName.Text =RowSubSys[3].ToString();
                        
this.rtxt_SubInfo.Text=RowSubSys[5].ToString();
                    }

                    comm.CommandText
="select * from AutoSystemInfo where AutoSysID =";
                    comm.CommandText
+="'"+this.treeView1.SelectedNode.Parent.Text.ToString().Trim().Substring(0,10)+"'";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"SubSysSysID");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"SubSysSysID");
                        conn.Close();
                    }

                    
foreach(DataRow RowSubSysSysID in ds.Tables["SubSysSysID"].Rows)
                    
{
                        
this.txt_SysSubName.Text=RowSubSysSysID[4].ToString();
                    }

                    ds.Tables.Clear();
                    comm.CommandText 
="select TagID 列表ID ,AutoSubSysID 子系统ID,TagCode 节点编码,TagName 节点名称,TagType 节点类型,TagValueType 数据类型,TagMaxValue 最大值,TagMinValue 最小值,TagAlartMax 报警最大值 ,TagAlartMin 报警最小值,TagSaveInterval 存储时间间隔,TagSaveIgBand 存储变化盲区,CreateTime 创建时间,TimeStampMode 时戳确定方式,TagAlartPos 报警点,TagAlartPosDes 报警点描述,Description 描述 from TagInfo where AutoSubSysID =";
                    comm.CommandText
+="'"+this.treeView1.SelectedNode.Text.ToString().Trim().Substring(0,10)+"'";
                    da 
= new OracleDataAdapter(comm.CommandText,conn);
                    
if(conn.State.ToString()=="Open")
                    
{
                        da.Fill(ds,
"Tag_SubSys");
                        conn.Close();
                    }

                    
else
                    
{
                        conn.Open();
                        da.Fill(ds,
"Tag_SubSys");
                        conn.Close();
                    }

                    takeHideValue(
"Tag_SubSys");
                    
this.dataGrid1.DataSource =ds.Tables["Tag_SubSys"];
                }

            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message,title,MessageBoxButtons.OK,MessageBoxIcon.Information);
            }


        }

在DataSet中手动添加节点:
private  DataTable IOServerNode  =   new  DataTable( " IOServerNode " );
        
private  DataTable SubSysNode  =   new  DataTable( " SubSysNode " );
        
private  DataTable SysNode  =   new  DataTable( " SysNode " );
IOServerNode.Columns.Add(
" IOServerID " , typeof ( string ));
            IOServerNode.Columns.Add(
" IOServerName " , typeof ( string ));
            SysNode.Columns.Add(
" AutoSysID " , typeof ( string ));
            SysNode.Columns.Add(
" AutoSysName " , typeof ( string ));
            SysNode.Columns.Add(
" IOServerID " , typeof ( string ));
            SubSysNode.Columns.Add(
" AutoSubSysID " , typeof ( string ));
            SubSysNode.Columns.Add(
" AutoSubSysName " , typeof ( string ));
            SubSysNode.Columns.Add(
" AutoSysID " , typeof ( string ));
            dsNode.Tables.Add(IOServerNode);
            dsNode.Tables.Add(SysNode);
            dsNode.Tables.Add(SubSysNode);
//             DataRowCollection rc;
                    
//             DataRow newRow;
                    
//             object [] rowValues = new object[2];
                    
//             rowValues[0]="0000000210";
                    
//             rowValues[1]="这是一个测试";
                    
//             rc =dsNode.Tables["IOServerNode"].Rows;
                    
//             newRow =dsNode.Tables["IOServerNode"].NewRow();
                    
//             newRow = rc.Add(rowValues);
                    
// dsNode.Tables["IOServerNode"].ImportRow(newRow);

你可能感兴趣的:(控件)