//简单数据视图 
import win.ui;
/*DSG{{*/
var winform = win.form(text="简单数据视图 - 双击编辑单元格(可指定只读列)";right=1031;bottom=712)
winform.add(
edit={cls="edit";left=25;top=584;right=997;bottom=693;edge=1;multiline=1;z=2};
listview={cls="listview";left=24;top=27;right=996;bottom=555;edge=1;z=1}
)
/*}}*/

import win.ui.grid;
var grid = win.ui.grid(winform.listview);//创建数据视图
grid.readonlyColums = { [1] = true };//设置禁止编辑的列

import sqlite;
var db = sqlite("/testParameters.db");
db.exec( "CREATE TABLE IF NOT EXISTS @tablename(id INTEGER PRIMARY KEY, date, title);",{
    tablename = "myTableName"
} ) 

var cmd = db.prepare("INSERT INTO myTableName VALUES (NULL,@date,@title);" );
for(i=1;10;1){
	cmd.step(
    	date = time().addday( math.random(1,30));
    	title = string.random(15);
 	)
}

//直接支持常用数据库的getTable函数返回的数据格式
var dataTable = db.getTable("SELECT * FROM [myTableName] ");

//可选自定义显示列名
//grid.setColumns({"ID";"日期";"标题"})

////直接让liswview控件显示数据库的查询结果
grid.setTable( dataTable )

//编辑变更值会触发下面的事件
grid.onEditChanged = function(text,iItem,iSubItem){
	var name = dataTable.fields[iSubItem]
	winform.edit.print(dataTable[iItem][name],"-->",text);
	dataTable[iItem][name] = text;
} 

/*
用户点击列头排序时会触发下面的事件，
cloumn为例号，desc参数指定是否倒序，返回true允许当前列排序
*/
grid.onSortColumn = function(cloumn,desc){
	var name = dataTable.fields[cloumn]
	
	if(name=="date"){
		if(desc){
			table.sort(dataTable,function(b){
				return time(owner[name]) < time(b[name])
			})	
		}
		else {
			table.sort(dataTable,function(b){
				return time(owner[name]) > time(b[name])
			})
		}
		
		grid.setTable( dataTable )
		return true;		
	} 
	else {
		winform.msgboxErr("排序请点击date列")
	} 
}

winform.show() 
win.loopMessage();
