Results 1 to 12 of 12

Thread: Load all db tables in treeview??[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Load all db tables in treeview??[Resolved]

    I couldn't figure out how to do this ? I have only VB.NET version ! I've been trying to convert it for a while but to no avail . anyone show me how plz ?

    VB Code:
    1. 'TreeView
    2. Public Overloads Shared Sub LoadTables(ByVal TreView As TreeView)
    3.                 OpenDB.OpenDB()
    4.                 Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
    5.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    6.                
    7.                 CloseDB.CloseDB()
    8.                 If Tables.Rows.Count > 0 Then
    9.                     TreView.BeginUpdate()
    10.                     'add tables as child nodes
    11.                     Dim dr As DataRow
    12.                     For Each dr In Tables.Rows
    13.                         'you can add icons here if you want
    14.                         TreView.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
    15.                     Next
    16.                     TreView.EndUpdate()
    17.                 End If
    18.                 Tables = Nothing
    19.             End Sub

    C#
    Code:
    //TreeView
    			public static void LoadTables (System.Windows.Forms.TreeView TreView) 
    			{
    			OpenDB.OpenDB1() ;
    			DataTable Tables = MyConnection().GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
    			CloseDB.CloseDB1();
    				
    				if (Tables.Rows.Count > 0 ){
    			TreView.BeginUpdate();
          
    					foreach (DataRow dr in Tables.Rows ) {
    TreView.Nodes.Add(new TreeNode (dr(("TABLE_NAME"))));
     TreView.EndUpdate ();
    					}
    					Tables=null;
    			
    				}		
    
    			}
    This code keeps throwing an error says :
    Class1.cs(72): 'dr' denotes a 'variable' where a 'method' was expected
    I tried all possible ways but I don't get it to work ? Is my code correct ??
    btw , I can load tables in listbox , combobox , checkedlistbox !

    Thanks
    Last edited by Pirate; Mar 19th, 2003 at 10:11 AM.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here is an example of how to populate the treeview control from a datatable:

    Code:
    private void button1_Click(object sender, System.EventArgs e) {
        DataTable dt = new DataTable();
        DataRow newRow = dt.NewRow();
        dt.Columns.Add("TABLE_NAME", typeof(System.String));
        newRow[0] = "Account";
        dt.Rows.Add(newRow);
        LoadTreeView(dt);
    }
    
    private void LoadTreeView(DataTable dt) {
        if(dt.Rows.Count > 0) {
            treeView1.BeginUpdate();
            foreach(DataRow r in dt.Rows) {
                treeView1.Nodes.Add(new TreeNode(r["TABLE_NAME"].ToString()));
            }
            treeView1.EndUpdate();
        }
    }

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanks Lethal for making me look at "[].tostring()" . I was messing it up . I just changed one line to

    Code:
    TreView.Nodes.Add(new TreeNode (dr["TABLE_NAME"].ToString ()));
    Last edited by Pirate; Mar 19th, 2003 at 10:13 AM.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    No problem....I knew as soon as you looked at the code I posted you would realize the problem.

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    How do you translate this into C# ??
    VB Code:
    1. Dim flds(MyDataset.Tables(0).Columns.Count - 1) As DataColumn

    I did this but not working

    Code:
    DataColumn flds(Property.MyDataset.Tables[0].Columns.Count -1);

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Code:
    DataColumn[] flds = new DataColumn[Property.MyDataset.Tables[0].Columns.Count -1];

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Why this aint working ?? As I know about VB.NET, it's not necessary to instantiate new DataColumn obj ??

    Code:
    DataColumn[Property.MyDataset.Tables[0].Columns.Count -1] flds;
    Thanks Lethal

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I got the following error says "Additional information: Destination array was not long enough. Check destIndex and length, and the array's lower bounds"

    Code:
    public static void LoadColumns (System.Windows.Forms.ListBox LstBox,string TableStr) {
    OpenDB.OpenDB1 ();
    Property.SQLStr ="Select * from " + TableStr ;
    OleDbDataAdapter myadapter = new OleDbDataAdapter (Property.SQLStr,Property.MyConnection());
    myadapter.Fill (Property.MyDataset ,TableStr);
    DataColumn[] flds = new DataColumn[Property.MyDataset.Tables[0].Columns.Count -1];
    Property.MyDataset.Tables[0].Columns.CopyTo (flds,0);
    
    
    //here it starts
    if (LstBox.Items.Count==0) {
    LstBox.Items.AddRange (flds);
    }else{
    //ends here 
    
    
    }
    Property.MyDataset.Reset();
    MyDB.CloseDB.CloseDB1(); 
    myadapter=null;
    			}
    What the hell is wrong again??
    Thanx for any help

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Pirate

    Code:
    DataColumn[Property.MyDataset.Tables[0].Columns.Count -1] flds;
    any way around to get this working ?? I'm stuck .

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Code:
    DataColumn[] flds = new DataColumn[Property.MyDataset.Tables[0].Columns.Count];
    Arrays in C# are zero based. Therfore, if the count property returns 10, you can access the 0-9 indices.

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Pirate
    I got the following error says "Additional information: Destination array was not long enough. Check destIndex and length, and the array's lower bounds"

    Code:
    //here it starts
    if (LstBox.Items.Count==0) {
    LstBox.Items.AddRange (flds);
    }else{
    //ends here 
    }
    			}
    Why it keeps throwing error on this line
    Thanx Lethal for any help

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    aaaah it's been solved now . wow !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width