Sign in to follow this  
Followers 0
nevodj

2D Array into GUI Listview

22 posts in this topic

Hello,

I can't find in the forum how I can pass a 2D array (created by _Sql_GetTable2d) into a list view in my GUI.

Obviously I can do it with _ArrayDisplay but I want it in a neat list view in my GUI.

Sure there is a way to do this... but I can't find it!

Thanks

nevodj

Share this post


Link to post
Share on other sites



Here's how I did it in a media player that I'm working on:

For $n = 0 To UBound($aArray) - 1
        _GUICtrlListView_AddItem($ListView, $aArray[$n][0], -1, $n + 9999)
        For $s = 1 To UBound($aArray, 2) - 1
            _GUICtrlListView_AddSubItem($ListView, $n, $aArray[$n][$s], $s)
        Next
    Next

Obviously you'd use your own variable/array names in place of what I have here. BTW, in case you're wondering about this line _GUICtrlListView_AddItem($ListView, $aArray[$n][0], -1, $n + 9999), the $n + 9999 is because I'm using the Listview UDF to add lines to the listview, if you want the listview sortable, you need to add 9999 to the line number when creating a new item in the listview.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks BrewManNH!! That works great!

Quick question... how would I delete the first line of the array, or not add it to the list view as it contains the SQL column names which I dont want as my first line.

Thanks again...

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Replace

For $n = 0 To UBound($aArray) - 1
with
For $n = 1 To UBound($aArray) - 1
But make sure that the SQL command was run successfully and returned at least one record.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I did try that... but it messes up the array and only populates the first column of the array.

E.G I am doing SELECT STOCKCODE, QUANTITY FROM STOCKITEMS

It returns every row but only the stockcode.

Share this post


Link to post
Share on other sites

And the column names are still displayed?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

No the column names are not still displayed...

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This should work:

For $n = 0 To UBound($aArray) - 1
    _GUICtrlListView_AddItem($ListView, $aArray[$n][0], -1, $n + 9999)
    For $s = 0 To UBound($aArray, 2) - 1
        _GUICtrlListView_AddSubItem($ListView, $n, $aArray[$n][$s], $s+1)
    Next
Next

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Nope... sorry

Share this post


Link to post
Share on other sites

The code posted will populate a Listview with the contents of a 2D array starting at the second element of the array ([1]). If it's not doing that for you then there's a problem elsewhere in your code. "Nope... sorry" isn't very helpful as an error message. What's not working, and how is it not working?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks.... The code Water posted screws my array up totally like so:

Code Description Qty1 Qty2

1234 1234 Item1234 Qty1

4567 4567 Item1234 Qty1

It should be like

Code Description Qty1 Qty2

1234 Item1234 Qty1 Qty2

4567 Item1234 Qty1 Qty2

Don't know if that makes sense... it is putting the stockcode in the description column as well.

It displays fine using _ArrayDisplay.

Thanks!!

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Correct Array (with BrewManNH code)

<TABLE border=1 width=700>
<TBODY>
<TR>
<TD>Stockcode</TD>
<TD>Description</TD>
<TD>In Stock</TD>
<TD>On order</TD></TR>
<TR>
<TD>1234</TD>
<TD>Item 1234</TD>
<TD>500</TD>
<TD>100</TD></TR>
<TR>
<TD>5678</TD>
<TD>Item 5678</TD>
<TD>400</TD>
<TD>50</TD></TR></TBODY></TABLE>

Incorrect Array (with Water code)

<TABLE border=1 width=700>
<TBODY>
<TR>
<TD>Stockcode</TD>
<TD>Stockcode</TD>
<TD>Description</TD>
<TD>Instock</TD></TR>
<TR>
<TD>1234</TD>
<TD>1234</TD>
<TD>Item 1234</TD>
<TD>500</TD></TR>
<TR>
<TD>5678</TD>
<TD>5678</TD>
<TD>Item 5678</TD>
<TD>400</TD></TR></TBODY></TABLE>

EDIT: SORRY I'M TRYING TO GET IT TO SHOW IN A TABLE FOR YOU...

Edited by nevodj

Share this post


Link to post
Share on other sites

I see the problem, in my code, just change the For $n = 0 to UBound($aArray) - 1 to For $n = 1 to UBound($aArray) - 1, don't change the second For ... Next loops numbers from the one I posted.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Must be something wrong with my array... when i do that i only get the stockcode (the first column in the array) coming thru...

thanks for your help anyway... I will keep playing with it.

Share this post


Link to post
Share on other sites

I see the problem, in my code, just change the For $n = 0 to UBound($aArray) - 1 to For $n = 1 to UBound($aArray) - 1, don't change the second For ... Next loops numbers from the one I posted.

Why shouldn't the second loop start with 0? Otherwise you drop the first column. The data the OP posted shows that column 0 has data to be displayed.

Or did I miss something?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

@nevodj

You're a little fuzzy about what you exactly run (and want), but anyway.

In the situation you describe and if this is for populating a listview, I'd switch to a much more simple approach: use SQLite to format rows so that they directly insert in your listview.

_SQLite_GetTable($hDB, "select itemcode || '|' || itemdesc || '|' || qty1 || '|' || qyt2 from stock where ...", $rows, $nrows, $ncols)
For $i = 2 To UBound($rows) - 1
  GUICtrlCreateListViewItem($rows[$i], $ListView)
Next

Rename table(s), columns, variables; add salt, error checks, bells & whistles.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Why shouldn't the second loop start with 0? Otherwise you drop the first column. The data the OP posted shows that column 0 has data to be displayed.

Or did I miss something?

Because you're using the first column of the "row" of the array in the first column of the listview, if you reuse the first column you'll end up doubling up the first value in the first 2 columns of the listview. The second For...Next loop is to add subitems to the LV rows.

Array[0] = Column1 <<<<<<< Item Column 1 of listview

Array[1] = Column2 <<<<<<< Subitem Column 2 of listview

Array[2] = Column3 <<<<<<< Subitem Column 3 of listview


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Oh, got a big cup of coffee and now understand what was wrong in my version :D

Thanks for the explanation!


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The headers should be defined at listview creation. This is much simpler than using SQL column aliases in order to have user-friendly names.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#20 ·  Posted

So I'm having the same problem as the OP and the solution proposed is giving me the same results he reported. I structured my code as suggested but what it does is it lists the database column names as the first line of the list view.

The list view resides in a tab of my application and starts out empty. I then enter a mac address, click "OK" and perform the following. The code suggested works great except that in my list view I already spell out the column names that I want. The below code executes and the first line in my listview reads:

MAC Service State Startup Pathname

the next line reads

00:12:23:24:24 Adobe??? Started Auto c:/?????????

The second line contains the actual data that I want to display. How do I keep from returning the column names of the database as the fist line?

Changing the 0 to a 1 as suggested gives me the same result as the OP in that only the first column of data is returned. He said pretty much what I have but I think he gave up trying to communicate his issue.

Local $list, $aResult, $iRows, $iColumns, $n, $aArray

_SQLite_GetTable2d(-1, "SELECT * FROM SERVICES WHERE MAC = '"& $MAC &"';", $aArray, $iRows, $iColumns)

For $n = 0 To UBound($aArray) - 1

_GUICtrlListView_AddItem($ListView1, $aArray[$n][0], -1, $n + 9999)

For $s = 0 To UBound($aArray, 2) - 1

_GUICtrlListView_AddSubItem($ListView1, $n, $aArray[$n][$s], $s)

Next

Next

I hope the additional explanation and examples help.

Thanks a million!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0