Solved

how to populate a datatable with values from a listview

Posted on 2004-09-27
18
501 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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