|
-
Mar 10th, 2003, 06:26 AM
#1
Thread Starter
Sleep mode
Pass DataTable to listview ??[Resolved by__Edneeis]
I did this with Combobox , Listbox , CheckedListBox because all have the same property (DataSource) but ListView and TreeView don't have !
How can I pass DataTable to those two ?
Thanx for any help !
Last edited by Pirate; Mar 11th, 2003 at 02:22 PM.
-
Mar 10th, 2003, 07:08 AM
#2
Thread Starter
Sleep mode
I guess I have to explain more
This how I did with Combobox or Listbox or CheckListBox
VB Code:
dim table as DataTable
Combobox1.DataSorce =DataTable
I need to pass DataTable to ListView :
Listview1. ....=DataTable
any idea ?
-
Mar 10th, 2003, 12:44 PM
#3
Thread Starter
Sleep mode
-
Mar 10th, 2003, 01:21 PM
#4
I don't believe so. You'll have to create your own type of binding solution for the TreeView and ListView.
-
Mar 10th, 2003, 01:58 PM
#5
Thread Starter
Sleep mode
Edneeis , thank you for replying me . Do you remember the code you sent me that loads all tables in database .It was great and efficient . I need to do the same but with Listview and Treeview ?? I've no idea about this hell .any help would be appreciated .
Thanx again
-
Mar 10th, 2003, 03:23 PM
#6
Thread Starter
Sleep mode
Edneeis , this is your code which loads all tables in a database :
VB Code:
Public Overloads Shared Sub LoadTables(ByVal LstBox As ListBox)
OpenDB.OpenDB()
Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable _
(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
CloseDB.CloseDB()
LstBox.DataSource = Tables
LstBox.DisplayMember = "TABLE_NAME"
Tables = Nothing
End Sub
-
Mar 10th, 2003, 05:56 PM
#7
Here is the TreeView and I'll let you disect it and do the ListView one.
VB Code:
Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
'see if connection is already open
Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
'if it wasn't already open then open the connection
If IOpened Then cnn.Open()
Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
'if the connection was opened in this method then close it
If IOpened Then cnn.Close()
'"TABLE_CATALOG" is the database name
'"TABLE_NAME" is the table name
'add database and tables to tree
If Tables.Rows.Count > 0 Then
'make db as root node
'you can add icons here if you want
Dim root As New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
'add tables as child nodes
Dim dr As DataRow
For Each dr In Tables.Rows
'you can add icons here if you want
root.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
Next
'add all info to tree
tvw.Nodes.Add(root)
End If
Tables = Nothing
End Sub
'example of use
Dim cnn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
& "Persist Security Info=False;Initial Catalog=MHC_NET;Data Source=mhc2")
LoadTables(TreeView1, cnn)
Last edited by Edneeis; Mar 11th, 2003 at 03:07 PM.
-
Mar 10th, 2003, 05:57 PM
#8
Lively Member
I have a somewhat similar problem too. I can get the listview to load but i can't get it to display just the info i want with the dataview you showed me Edneeis.
I've got the sample program to show the correct data for each of the paddocks in a datagrid ok, but i don't particularly like datagrids and was trying to get this to work with the listview instead.
I made a new dataview called dvCropFilter. Here's the code to load a listview, hopefully it will help you pirate or point you in the right direction.
Also, how do you stop the flickering when a datagrid loads, if you notice listviews have a "BeginUpdate" and "EndUpdate". Does a datagrid have something similar to this? I couldn't find it.
Code:
'Freeze the listview
lvwCropData.BeginUpdate()
'Display the paddocks in the List View
lvwCropData.Items.Clear()
Dim dr As DataRow
'Declare a Sub Item of the ListView
Dim oItem As ListViewItem
Dim osItem As ListViewItem.ListViewSubItem
' If Not ds Is Nothing Then
For Each dr In ds.Tables("Rotations").Rows
'Make the New
oItem = New ListViewItem()
'Id for the Item - Was Key in vb6
oItem.Tag = dr("RotationsID")
osItem = New ListViewItem.ListViewSubItem()
oItem.Text = dr("RotationsID")
osItem.Text = dr("FarmName")
oItem.SubItems.Add(osItem)
osItem = New ListViewItem.ListViewSubItem()
osItem.Text = dr("PaddockNum")
oItem.SubItems.Add(osItem)
osItem = New ListViewItem.ListViewSubItem()
osItem.Text = dr("CropType")
oItem.SubItems.Add(osItem)
osItem = New ListViewItem.ListViewSubItem()
osItem.Text = dr("Year")
oItem.SubItems.Add(osItem)
lvwCropData.Items.Add(oItem)
Next
'End If
'Now redraw
lvwCropData.EndUpdate()
-
Mar 11th, 2003, 12:48 PM
#9
Thread Starter
Sleep mode
First , Thank you Edneeis
I got this error when running your code saying :
Cast from "DBNULL" to type "String" is not valid ??
here , this code is highlighted . What's wrong ?
VB Code:
Dim root As New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
Thank you for your time Edneeis !
-
Mar 11th, 2003, 01:14 PM
#10
What kind of database are you using? I am using SQL and that field holds the Database name, but maybe if you are using something else it is NULL. If you don't want a root database node in the treeview then here is a modified version for either just tables or the name if there.
VB Code:
Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
'see if connection is already open
Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
'if it wasn't already open then open the connection
If IOpened Then cnn.Open()
Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
'if the connection was opened in this method then close it
If IOpened Then cnn.Close()
'"TABLE_CATALOG" is the database name
'"TABLE_NAME" is the table name
'add database and tables to tree
If Tables.Rows.Count > 0 Then
'make db as root node
Dim root As TreeNode
If IsDBNull(Tables.Rows(0)("TABLE_CATALOG")) Then
'no db name listed so use generic
'you can add icons here if you want
'root = New TreeNode("Database", 0, 0)
root = New TreeNode("Database")
Else
'root as db name
'root = New TreeNode(Tables.Rows(0)("TABLE_CATALOG"), 0, 0)
root = New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
End If
'add tables as child nodes
Dim dr As DataRow
For Each dr In Tables.Rows
'you can add icons here if you want
'root.Nodes.Add(New TreeNode(dr("TABLE_NAME"), 1, 1))
root.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
Next
'add all info to tree
tvw.Nodes.Add(root)
End If
Tables = Nothing
End Sub
Last edited by Edneeis; Mar 11th, 2003 at 03:01 PM.
-
Mar 11th, 2003, 01:19 PM
#11
Thread Starter
Sleep mode
I have MS DB (XP version) . I will try it now .
Thank Edneeis
-
Mar 11th, 2003, 01:25 PM
#12
Thread Starter
Sleep mode
You did it again and again . It's working like a charm .
but Should I have the root node "DataBase" at the top ? I just want get rid of it
Thank you so so much
-
Mar 11th, 2003, 02:20 PM
#13
Thread Starter
Sleep mode
Edneeis , That's enough . This was rude . Thank you for giving me some of your time.
-
Mar 11th, 2003, 02:56 PM
#14
Well without any heirarchy I'm not sure why you'd want to use a treeview, why not just use a listbox? None the less here it is without the database root:
VB Code:
Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
'see if connection is already open
Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
'if it wasn't already open then open the connection
If IOpened Then cnn.Open()
Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
'if the connection was opened in this method then close it
If IOpened Then cnn.Close()
'"TABLE_CATALOG" is the database name
'"TABLE_NAME" is the table name
'add database and tables to tree
If Tables.Rows.Count > 0 Then
tvw.BeginUpdate()
'add tables as child nodes
Dim dr As DataRow
For Each dr In Tables.Rows
'you can add icons here if you want
'root.Nodes.Add(New TreeNode(dr("TABLE_NAME"), 1, 1))
tvw.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
Next
tvw.EndUpdate()
End If
Tables = Nothing
End Sub
-
Mar 11th, 2003, 03:07 PM
#15
Thread Starter
Sleep mode
I will try it right now .
-
Mar 11th, 2003, 03:13 PM
#16
Thread Starter
Sleep mode
exactly what I loved to have . Thank you Edneeis . I owe you a lot .
-
Mar 11th, 2003, 03:19 PM
#17
Thread Starter
Sleep mode
I'm doing all this because I'm overloading all my database functions . So next time I build my projs I have it ready and should accept all controls (listbox , combobox, listview, etc )
and now I'm done , I can build this in C# so that I can make use of functions documentations
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
|