• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

how to populate a datatable with values from a listview

Hi,

The listview has variable number of rows and columns.

Thanks for any help.
0
MsFox
Asked:
MsFox
  • 7
  • 4
  • 2
  • +3
1 Solution
 
123654789987Commented:
Dim dt as DataTable

dt = listview.DataSource
0
 
MsFoxAuthor Commented:
i tried  this but there's no datasource property for listview
0
 
RonaldBiemansCommented:
How is your listview filled ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wguerramCommented:
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
 
MsFoxAuthor Commented:
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
 
eozz_2000Commented:
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
 
MsFoxAuthor Commented:
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
 
wguerramCommented:
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
 
eozz_2000Commented:
I agree, and before populating first clear the list... well, I always do that
0
 
MsFoxAuthor Commented:
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
 
MsFoxAuthor Commented:
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
 
wguerramCommented:
Can you post your code for adding items?
0
 
MsFoxAuthor Commented:
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
 
wguerramCommented:
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
 
MsFoxAuthor Commented:
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
 
ee_ai_constructCommented:
Question answered by asker or dialog valuable.
Closed, 30 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now