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 !
Printable View
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 !
I guess I have to explain more :D
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 ?
anyone plz ??
I don't believe so. You'll have to create your own type of binding solution for the TreeView and ListView.
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
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
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)
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()
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 ?
Thank you for your time Edneeis !:)VB Code:
Dim root As New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
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
I have MS DB (XP version) . I will try it now .
Thank Edneeis
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
Edneeis , That's enough . This was rude . Thank you for giving me some of your time.
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
I will try it right now . :D
exactly what I loved to have . Thank you Edneeis . I owe you a lot :( .
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 :D