|
-
Mar 18th, 2003, 03:03 PM
#1
Thread Starter
Sleep mode
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:
'TreeView
Public Overloads Shared Sub LoadTables(ByVal TreView As TreeView)
OpenDB.OpenDB()
Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
CloseDB.CloseDB()
If Tables.Rows.Count > 0 Then
TreView.BeginUpdate()
'add tables as child nodes
Dim dr As DataRow
For Each dr In Tables.Rows
'you can add icons here if you want
TreView.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
Next
TreView.EndUpdate()
End If
Tables = Nothing
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.
-
Mar 18th, 2003, 08:18 PM
#2
PowerPoster
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();
}
}
-
Mar 19th, 2003, 09:45 AM
#3
Thread Starter
Sleep mode
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.
-
Mar 19th, 2003, 05:39 PM
#4
PowerPoster
No problem....I knew as soon as you looked at the code I posted you would realize the problem.
-
Mar 19th, 2003, 09:02 PM
#5
Thread Starter
Sleep mode
How do you translate this into C# ??
VB Code:
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);
-
Mar 19th, 2003, 09:09 PM
#6
PowerPoster
Code:
DataColumn[] flds = new DataColumn[Property.MyDataset.Tables[0].Columns.Count -1];
-
Mar 19th, 2003, 09:52 PM
#7
Thread Starter
Sleep mode
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
-
Mar 19th, 2003, 10:11 PM
#8
Thread Starter
Sleep mode
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
-
Mar 20th, 2003, 10:03 AM
#9
Thread Starter
Sleep mode
Originally posted by Pirate
Code:
DataColumn[Property.MyDataset.Tables[0].Columns.Count -1] flds;
any way around to get this working ?? I'm stuck .
-
Mar 20th, 2003, 11:10 AM
#10
PowerPoster
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.
-
Mar 20th, 2003, 11:20 AM
#11
Thread Starter
Sleep mode
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
-
Mar 20th, 2003, 01:33 PM
#12
Thread Starter
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|