I've also created an improved version of the showtable listview function.
I use this frequently in my applications.
I'd like to see it included in this library.
Improvements are:
1. column resizes correctly and accounts for column header and column data.
2. you can automatically hide columns by adding a $ to the variable name (e.g. select id$, name, address from table1).
3. listview can remember previous selection.
4. integer columns are handled correctly for sorting
;============================================================
; fill listview with results from query
; note: the current data in the listview is replaced with the new data
;
; inputs:
; listviewname: the name of the listview to fill
;
; table: provide a table object which is usually generated from mysql call.
; Example:
; datatable := db.Query(sql)
;
; Underscores are automatically removed from aliasname before displaying as column headers
;
; To hide a column put a $ at the end of the column name
; Example:
; select id as id$, name from table1
;
; selectmode: (optional) Important when refreshing an existing listview. Set how to re-select the same row.
; 0 = no re-select (default)
; 1 = select by column 1 value (column 1 is assumed to be unique)
; 2 = select by row number (recommended only if your list is relatively static)
;
;============================================================
lvfill(listviewname, table, selectmode=0)
{
;-------------------------------------------
; delete all rows in listview
;-------------------------------------------
GuiControl, -Redraw, %listviewname% ; to improve performance, turn off redraw then turn back on at end
Gui, ListView, %listviewname% ; specify which listview will be updated with LV commands
;-------------------------------------------
; remember current selection in listview
;-------------------------------------------
if (selectmode = 1) {
column1value := ""
selectedrow := LV_GetNext(0) ; get current selected row
if selectedrow |= 0
LV_GetText(column1value, selectedrow, 1) ; get column 1 value for current row
} else if (selectmode = 2) {
selectedrow := LV_GetNext(0) ; get current selected row
}
;-------------------------------------------
; delete any pre-existing rows and columns in listview
;-------------------------------------------
LV_Delete() ; delete all rows in listview
Loop, % LV_GetCount("Column") ; delete all columns in listview
LV_DeleteCol(1)
;-------------------------------------------
; create new columns
;-------------------------------------------
for each, colName in table.Columns
{
colName := RegExReplace(colName , "_", " ") ; remove underscores from column names
LV_InsertCol(A_Index,"", colName)
}
;columnCount := table.Columns.Count()
;-------------------------------------------
; insert rows
;-------------------------------------------
for each, row in table.Rows
{
rowNum := LV_Add("", "")
for each, colName in table.Columns
LV_Modify(rowNum, "Col" . A_index, row[A_index])
}
;-------------------------------------------
; use first row values to set integer columns
;-------------------------------------------
if table.Rows.Count() ; only if table contains rows
for each, colName in table.Columns
{
data := table[1][A_Index] ; table[row][column]
StringReplace, data, data, % " KB",, ; remove " KB" so this column can be interpreted as an integer
if data is integer
LV_ModifyCol(A_Index, "Integer") ; For sorting purposes, indicate column is an integer.
}
;-------------------------------------------
; autosize columns: should be done outside the row loop to improve performance
;-------------------------------------------
LV_ModifyCol() ; Auto-size each column to fit its contents.
for each, colName in table.Columns
{
LV_ModifyCol(A_Index,"AutoHdr") ; Autosize headers (does last header need this?)
if RegExMatch(colName, "\$$") ;If there is a $ at end of column name, that indicates a hidden column
LV_ModifyCol(A_Index,0) ; set width to 0 to create hidden column
}
Gui, Submit, NoHide ; update v control variables
;-------------------------------------------
; re-select row in listview
;-------------------------------------------
if (selectmode = 1) { ;reselect row by column1value
if (column1value != "") {
Loop % LV_GetCount() ; loop through all rows in listview to find column1value
{
LV_GetText(value, A_Index, 1) ; get column1 value for current row
If (value = column1value) {
LV_Modify(A_Index, "+Select +Focus") ; select originally selected row in list
break
}
}
}
} else if (selectmode = 2) { ; reselect row by row number
if (selectedrow != 0)
LV_Modify(selectedrow, "+Select +Focus") ; select originally selected row in list
}
GuiControl, +Redraw, %listviewname% ; to improve performance, turn off redraw at beginning then turn back on at end
Return
}