Link to home
Start Free TrialLog in
Avatar of Rick Becker
Rick BeckerFlag for United States of America

asked on

How do I output data from ListView Control with fields not in same order (or as many) as the ListView Hearder

OK Greetings..

I have done some searching and I have not quite found the answer to my issue so I guess I will ask the question here. (I'll include code snippets)

(1) I use Visual Studio 2017 express
(2) I Use Visual Basic .NET
(3) I am creating a Windows Application

OK.. I have a ListView Control  contained with a TabControl on a Form... The ListView has a Header of 40 fields and I can populate as many rows as I desire each with 40 fields of data.. (all working).

I need to get each row from the list and output that row data to a .TXT file but NOT All of the fields of data and NOT in the same order as represented in the ListView Header.

I actually have most all of this working EXCEPT my code will output a Duplicate record for the number of records within the ListView control..  Example if I have 3 rows of data and a header record  it will successfully output the desired number of data fields in the desired order for the first row of data then it will REPEAT/DUPLICATE that row of data for the number of rows in the ListView Control and then one more for the Header row.. ie 4 in the current example..

The following code snippet  is what I currently have and need a bit of help. Please note that I load the Header field names into an Array and then use that array
to INDEX into the ROW Item data.

BTW I hope that this is the right way to ask and or present questions to EE.. If not please let me know.
Thanks in advance.

Rick
--------------------------------------------------------------------------------------------------------------------------------------
'##########################################################################
        'Loop through the ListView Header values and store them in an array that
        ' is later used as a way to index the Header Name and Value associated
        ' with that column header
        '#########################################################################

        ListViewFieldNameString = ""
        For IntI = 0 To StandardUPCQueryTable.Columns.Count - 1
            ListViewFieldNameString = ListViewFieldNameString & StandardUPCQueryTable.Columns(IntI).Text.ToString & "|"
        Next
        ListViewFieldNameArray = Split(ListViewFieldNameString, "|")
        '###############################################################################
        ' Now loop through the ListView data rows and select the required field data to
        ' build the listing file. Output each New listing on a separate line in the
        ' Newly create Listing file.
        '#################################################################################
       
        MsgBox("Grid has " & StandardUPCQueryTable.Items.Count.ToString & "lines")

        For Each item As ListViewItem In StandardUPCQueryTable.Items

            'Action(SiteID=US|Country=US|Currency=USD|Version=403|CC=UTF-8) field
            objWriter.Write("Add" & vbTab)

            'Product:ProductReferenceID - fiels
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "ReferenceID")

            objWriter.Write(StandardUPCQueryTable.Items(1).SubItems.Item(FieldIndex).Text.ToString & vbTab) 'this line worked

            'Product:IncludePreFilledItemInformation - field
            objWriter.Write("1" & vbTab)

            'Product:IncludeStockPhotoURL - field
            objWriter.Write("1" & vbTab)

            'CustomLabel - field - our SKU
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "CustomLabel")
            objWriter.Write(StandardUPCQueryTable.Items(1).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'ConditionID - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "ConditionID")
            ConditionIDString = StandardUPCQueryTable.Items(1).SubItems.Item(FieldIndex).Text.ToString
            objWriter.Write(GetConditionIDCode(ConditionIDString).ToString & vbTab)

            'ConditionDescription - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "ConditionDescription")
            objWriter.Write(StandardUPCQueryTable.Items(1).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'Binding/Category - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "Category")
            MediaBidingString = StandardUPCQueryTable.Items(1).SubItems.Item(FieldIndex).Text.ToString
            objWriter.Write(GetMediaBindingIdCode(MediaBidingString).ToString & vbTab)

            objWriter.Write(vbNewLine)
           
        Next

-------------------------------------------------------------------------------------------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rick Becker

ASKER

it_sage, here is a code snippet of how I fixed the issue I was having:
-------------------------------------------------------------------------------------------------------
MsgBox("Grid has " & StandardUPCQueryTable.Items.Count.ToString & "lines")

        For Rows = 1 To StandardUPCQueryTable.Items.Count - 1

            'Action(SiteID=US|Country=US|Currency=USD|Version=403|CC=UTF-8) field
            objWriter.Write("Add" & vbTab)

            'Product:ProductReferenceID - fiels
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "ReferenceID")
            objWriter.Write(StandardUPCQueryTable.Items(Rows).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'Product:IncludePreFilledItemInformation - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "IncludePreFilledItemInformation")
            objWriter.Write(StandardUPCQueryTable.Items(Rows).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'Product:IncludeStockPhotoURL - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "IncludeStockPhoto")
            objWriter.Write(StandardUPCQueryTable.Items(Rows).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'CustomLabel - field - our SKU
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "CustomLabel")
            objWriter.Write(StandardUPCQueryTable.Items(Rows).SubItems.Item(FieldIndex).Text.ToString & vbTab)

            'ConditionID - field
            FieldIndex = GetIndexFromArray(ListViewFieldNameArray, "ConditionID")
            ConditionIDString = StandardUPCQueryTable.Items(Rows).SubItems.Item(FieldIndex).Text.ToString
            objWriter.Write(GetConditionIDCode(ConditionIDString).ToString & vbTab)
       Next

Open in new window