Results 1 to 17 of 17

Thread: Pass DataTable to listview ??[Resolved by__Edneeis]

  1. #1

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

    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.

  2. #2

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I guess I have to explain more
    This how I did with Combobox or Listbox or CheckListBox
    VB Code:
    1. dim table as DataTable
    2. Combobox1.DataSorce =DataTable

    I need to pass DataTable to ListView :

    Listview1. ....=DataTable

    any idea ?

  3. #3

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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't believe so. You'll have to create your own type of binding solution for the TreeView and ListView.

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Edneeis , this is your code which loads all tables in a database :
    VB Code:
    1. Public Overloads Shared Sub LoadTables(ByVal LstBox As ListBox)
    2. OpenDB.OpenDB()
    3. Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable _
    4. (OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
    5. CloseDB.CloseDB()
    6. LstBox.DataSource = Tables
    7. LstBox.DisplayMember = "TABLE_NAME"
    8. Tables = Nothing
    9. End Sub

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is the TreeView and I'll let you disect it and do the ListView one.

    VB Code:
    1. Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
    2.         'see if connection is already open
    3.         Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
    4.         'if it wasn't already open then open the connection
    5.         If IOpened Then cnn.Open()
    6.         Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
    7.                    New Object() {Nothing, Nothing, Nothing, "TABLE"})
    8.         'if the connection was opened in this method then close it
    9.         If IOpened Then cnn.Close()
    10.         '"TABLE_CATALOG" is the database name
    11.         '"TABLE_NAME" is the table name
    12.         'add database and tables to tree
    13.         If Tables.Rows.Count > 0 Then
    14.             'make db as root node
    15.             'you can add icons here if you want
    16.             Dim root As New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
    17.             'add tables as child nodes
    18.             Dim dr As DataRow
    19.             For Each dr In Tables.Rows
    20.                 'you can add icons here if you want
    21.                 root.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
    22.             Next
    23.             'add all info to tree
    24.             tvw.Nodes.Add(root)
    25.         End If
    26.         Tables = Nothing
    27.     End Sub
    28.  
    29.         'example of use
    30.         Dim cnn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
    31.                 & "Persist Security Info=False;Initial Catalog=MHC_NET;Data Source=mhc2")
    32.         LoadTables(TreeView1, cnn)
    Last edited by Edneeis; Mar 11th, 2003 at 03:07 PM.

  8. #8
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    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()

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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:
    1. Dim root As New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
    Thank you for your time Edneeis !

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
    2.         'see if connection is already open
    3.         Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
    4.         'if it wasn't already open then open the connection
    5.         If IOpened Then cnn.Open()
    6.                 Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
    7.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    8.         'if the connection was opened in this method then close it
    9.         If IOpened Then cnn.Close()
    10.         '"TABLE_CATALOG" is the database name
    11.         '"TABLE_NAME" is the table name
    12.         'add database and tables to tree
    13.         If Tables.Rows.Count > 0 Then
    14.             'make db as root node
    15.             Dim root As TreeNode
    16.             If IsDBNull(Tables.Rows(0)("TABLE_CATALOG")) Then
    17.                 'no db name listed so use generic
    18.                 'you can add icons here if you want
    19.                 'root = New TreeNode("Database", 0, 0)
    20.                 root = New TreeNode("Database")
    21.             Else
    22.                 'root as db name
    23.                 'root = New TreeNode(Tables.Rows(0)("TABLE_CATALOG"), 0, 0)
    24.                 root = New TreeNode(Tables.Rows(0)("TABLE_CATALOG"))
    25.             End If
    26.             'add tables as child nodes
    27.             Dim dr As DataRow
    28.             For Each dr In Tables.Rows
    29.                 'you can add icons here if you want
    30.                 'root.Nodes.Add(New TreeNode(dr("TABLE_NAME"), 1, 1))
    31.                 root.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
    32.             Next
    33.             'add all info to tree
    34.             tvw.Nodes.Add(root)
    35.         End If
    36.         Tables = Nothing
    37.     End Sub
    Last edited by Edneeis; Mar 11th, 2003 at 03:01 PM.

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I have MS DB (XP version) . I will try it now .
    Thank Edneeis

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  13. #13

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

    Thumbs up

    Edneeis , That's enough . This was rude . Thank you for giving me some of your time.

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Shared Sub LoadTables(ByVal tvw As TreeView, ByVal cnn As OleDb.OleDbConnection)
    2.         'see if connection is already open
    3.         Dim IOpened As Boolean = Not (cnn.State = ConnectionState.Open)
    4.         'if it wasn't already open then open the connection
    5.         If IOpened Then cnn.Open()
    6.                 Dim Tables As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
    7.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    8.         'if the connection was opened in this method then close it
    9.         If IOpened Then cnn.Close()
    10.         '"TABLE_CATALOG" is the database name
    11.         '"TABLE_NAME" is the table name
    12.         'add database and tables to tree
    13.         If Tables.Rows.Count > 0 Then
    14.             tvw.BeginUpdate()
    15.             'add tables as child nodes
    16.             Dim dr As DataRow
    17.             For Each dr In Tables.Rows
    18.                 'you can add icons here if you want
    19.                 'root.Nodes.Add(New TreeNode(dr("TABLE_NAME"), 1, 1))
    20.                 tvw.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
    21.             Next
    22.             tvw.EndUpdate()
    23.         End If
    24.         Tables = Nothing
    25.     End Sub

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I will try it right now .

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    exactly what I loved to have . Thank you Edneeis . I owe you a lot .

  17. #17

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width