Basic TreeView

from the Artful MySQL Tips List


To implement a basic TreeView of a master-detail table pair:
String strConn = "Server=SRVR;uid=USR;pwd=PWD;database=nwib";
MySqlConnection conn = new MySqlConnection( strConn );
MySqlDataAdapter daCust = new MySqlDataAdapter( "Select * from customers", conn );
MySqlDataAdapter daOrds = new MySqlDataAdapter( "Select * from orders", conn );
daCust.Fill( ds, "Customers" );
daOrds.Fill( ds, "Orders" );
ds.Relations.Add( "Cust_Ord",
                  ds.Tables["Customers"].Columns["CustomerID"],
                  ds.Tables["Orders"].Columns["CustomerID"] );
foreach( DataRow dr in ds.Tables["Customers"].Rows ) { 
  TreeNode tn = new TreeNode( dr["CompanyName"].ToString() );
  foreach( DataRow drChild in dr.GetChildRows( "Cust_Ord" ) ) {
    tn.Nodes.Add( drChild["OrderID"].ToString() );
  }
  treeView1.Nodes.Add( tn );
}



Return to the Artful MySQL Tips page