如何实现排序功能
表单中,有一个表.
表中,有一个字段"日期". (datetime型)
要求是:
当单击此字段名,则升序排列
再次点击,则降序排列.
不知如何写代码,我用的是VFP 9.0 +SQL
[解决办法]
在这个表格日期列的标头 thisform.grid1.column1.header1.click 事件过程中,更改表格的recordsource 定义,改变其 sql 语句中的 order by 子句的排序方向.
[解决办法]
*OrderBy 自定义属性,为 .F. 表示 asc, .T. 表示 desc,初始化为.F.*HeaderCaption 自定义属性,用来保存上一次排序使用的列名,出事化为空(“”),字符型LOCAl array laEvEnts[1]Aevents(laEvEnts,0)obj_name = laEvEnts[1,1]lcHeaderName = obj_name.captionIf At('▲',lcHeaderName) != 0 Or At('▼',lcHeaderName) != 0 lcHeaderName = Substr(lcHeaderName, 1, Len(lcHeaderName) - 2) && 如果列名带有▼或▲ ,则先去除Endif If Alltrim(lcHeaderName) != Alltrim(thisform.HeaderCaption) thisform.OrderBy = .F. thisform.HeaderCaption = lcHeaderName For i = 2 To 19 lcColumn = 'Column' + Transform(i) lcTempString = thisform.container1.grid1.&lcColumn..header1.caption If At('▲',lcTempString) != 0 Or At('▼',lcTempString) != 0 && 当不是鼠标点击的列的时候,去除列标题中的 ▼或▲ thisform.container1.grid1.&lcColumn..header1.caption = Substr(lcTempString,1,Len(lcTempString) - 2) Exit Endif Next obj_name.caption = obj_name.caption + '▼'Else thisform.HeaderCaption = Substr(lcHeaderName,1,Len(lcHeaderName) - 2) If thisform.OrderBy = .F. thisform.OrderBy = .T. obj_name.caption = lcHeaderName + '▲' Else thisform.OrderBy = .F. obj_name.caption = lcHeaderName + '▼' Endif Endif thisform.container1.grid1.RecordSource = NULLIf Thisform.OrderBy = .F. && 已经从sql数据库中查询成成了 temp 临时表,用temp 作为查询源 Select * From temp Order By &lcHeaderName. Into Cursor tmp Select * From tmp Into Cursor tempElse Select * From temp Order By &lcHeaderName. Desc Into Cursor tmp Select * From tmp Into Cursor tempEndif thisform.container1.grid1.RecordSource = 'temp'thisform.container1.grid1.AutoFit thisform.container1.grid1.column1.Width = 0Go (thisform.lnLine)thisform.container1.grid1.SetFocus thisform.container1.grid1.Refresh