Solved

how to populate a datatable with values from a listview

Posted on 2004-09-27
18
490 Views
Last Modified: 2007-12-19
Hi,

The listview has variable number of rows and columns.

Thanks for any help.
0
Comment
Question by:MsFox
  • 7
  • 4
  • 2
  • +3
18 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 12166788
Dim dt as DataTable

dt = listview.DataSource
0
 
LVL 1

Author Comment

by:MsFox
ID: 12166808
i tried  this but there's no datasource property for listview
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12167219
How is your listview filled ?
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12174998
You have to declare a variable as DataTable and add columns to it

then you have to iterate the listview and insert the items into the table.
0
 
LVL 1

Author Comment

by:MsFox
ID: 12175484
My listview is filled from a dataset.  My problem is the result of the sorting on the listview items.  Since the resultset that that the listview referenced is the original resultset from the dataset, the index of every row remains the original despite the sorting.  So what I want is after sorting, I will re-populate the resultset with the sorted listbox values so it will referenced the new resultset.

I'm still pretty new to dotnet.  Below is my code to iterate, but I dont know how to populate the values back to the result set.

 For i = 0 To RecordList.Items.Count - 1
            For x = 0 To RecordList.Items(i).SubItems.Count - 1


            Next
 Next

Kindly point me to the right direction.

Thanks.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12176528
Why don't create a dataView with its dataSource to the table that you have populated, and then set its Sort property... after that, set your listView dataSource to the dataView.
0
 
LVL 1

Author Comment

by:MsFox
ID: 12177104
Ok, I tried eozz suggestion.

here is my code called from columnclick event of listview

Dim dv As DataView
GetRecordList(lResultSet)      --here I get the original resultset
dv = New DataView(lResultSet)      --I assigned it to a dataview
dv.Sort = lResultSet.Columns.Item(e.Column).ColumnName       --sort according to the column click by user
RecordList.AddRecords(dv.Table, RecordID)        -- re-populate listview

lResultSet is type datatable.

The problem is the sort doesn't work.  Can somebody point me what's wrong?

Thanks for any help.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12179649
You don't have to send the table of the view as parameter

RecordList.AddRecords(dv.Table, RecordID)        -- re-populate listview

just send the dataview

RecordList.AddRecords(dv, RecordID)        -- re-populate listview

To access rows of the view this is the code

ListView.ListItems.Add(dv(RowNumber)(ColumnNumber))
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:eozz_2000
ID: 12180116
I agree, and before populating first clear the list... well, I always do that
0
 
LVL 1

Author Comment

by:MsFox
ID: 12186800
Unfortunately, it didn't work either, so I tried another approach.  I copy the structure of the original resultset (type datatable) to a new resultset.  Then after sorting the listview, I am adding  each row to the new resultset based on primary key from the original resultset. Then I re-populate the listview based on my new resultset.  So far so good.

I am sorting the listiew when clicking the headers

The problem is in the addition of items. If I click on the column header of the primary key to sort, it doesn't produce an error, it fails on other columns.

Below is my code snippet that I use to populate the listview based on resultset.


 For lRowID As Integer = 0 To mResultSet.Rows.Count - 1
                For Each lColumnHeader As ColumnHeader In Columns
                    lData = mResultSet.GetData(lRowID, mFields(lColumnHeader.Index).ToString)
                    ' *** Add format of data here ***
                    If lColumnHeader.Index = 0 Then
                        lRowID = Items.Count
                        Items.Add(lData) -- FAILS HERE
                    Else
                        Items(lRowID).SubItems.Add(lData)
                    End If
                Next
next

It successfully added the first row, it fails in the second row. and I am getting:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: '1' is not a valid value for 'index'.
   at System.Windows.Forms.ListViewSubItemCollection.get_Item(Int32 index)
   at ClientAdmin.SortOrder.compare(Object x, Object y) in C:\CCAdmin\SortOrder.vb:line 18
   at System.Windows.Forms.ListView.CompareFunc(IntPtr lparam1, IntPtr lparam2, IntPtr lparamSort)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListView.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Thanks.



0
 
LVL 1

Author Comment

by:MsFox
ID: 12187328
I give the ListView capability to sort by a column when the column header is clicked and it works fine. The problem is when the listview gets refreshed, all items are cleared and then read from the new resultset and the listview is repopulated. Here it throws an exception when the 2nd item is added to the listview, apparently because it tries to compare it with the first to sort them out.

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: '1' is not a valid value for 'index'.
   at System.Windows.Forms.ListViewSubItemCollection.get_Item(Int32 index)
   at ClientAdmin.SortOrder.compare(Object x, Object y) in C:\CCAdmin\SortOrder.vb:line 18
   at System.Windows.Forms.ListView.CompareFunc(IntPtr lparam1, IntPtr lparam2, IntPtr lparamSort)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListView.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12189987
Can you post your code for adding items?
0
 
LVL 1

Author Comment

by:MsFox
ID: 12195714
This is the procedure I'm calling and I'm passing the new resultset


Public Sub AddRecords(ByVal resultSet As DataResult, ByVal recordID As String)
        Dim lSelectedRowID As Integer
        Dim lIDColumn As Integer
        Dim lData As String

        lSelectedRowID = -1
        If recordID = "" Then
            If SelectedItems.Count = 0 Then
                lSelectedRowID = 0
            Else
                lSelectedRowID = SelectedIndices(0)
            End If
        End If
        Items.Clear()

        mResultSet = resultSet
        If Not mResultSet Is Nothing Then
            lIDColumn = mResultSet.GetColumnID(mIDField)
            If lSelectedRowID > mResultSet.Rows.Count - 1 Then
                lSelectedRowID = mResultSet.Rows.Count - 1
            End If

            For lRowID As Integer = 0 To mResultSet.Rows.Count - 1
                For Each lColumnHeader As ColumnHeader In Columns
                    lData = mResultSet.GetData(lRowID, mFields(lColumnHeader.Index).ToString)
                    ' *** Add format of data here ***
                    If lColumnHeader.Index = 0 Then
                        lRowID = Items.Count
                        Items.Add(lData) --FAILS HERE FOR THE SECOND ROW
                    Else
                        Items(lRowID).SubItems.Add(lData)
                    End If
                Next
                If lSelectedRowID > -1 Then
                    If lRowID = lSelectedRowID Then
                        Items(lRowID).Selected = True
                        EnsureVisible(lRowID)
                    End If
                Else
                    If mResultSet.GetData(lRowID, lIDColumn) = recordID Then
                        Items(lRowID).Selected = True
                        EnsureVisible(lRowID)
                    End If
                End If
            Next
        End If

        If Items.Count = 0 Then
            RaiseEvent ListEmpty()
        ElseIf SelectedItems.Count = 0 Then
            Items(0).Selected = True
        End If
    End Sub

Thanks,
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12200038
I don't see nothing wrong in that line:

Items.Add(lData) --FAILS HERE FOR THE SECOND ROW


try placing a try catch to see the message

try
   Items.Add(lData) --FAILS HERE FOR THE SECOND ROW
catch ex as exception
   msgbox (ex.Message)
   msgbox (ex.StackTrace)
end try

and let me know what is the message.
0
 
LVL 1

Author Comment

by:MsFox
ID: 12256438
Sorry for not getting back to you immediately.  Actually, I already solved the problem.

The solution is not to call the procedure which re-populates the listview.  I just set the new resultset as source for the listview.

Anyway, thanks a lot for all your help.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12506861
Question answered by asker or dialog valuable.
Closed, 30 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now