当前位置:编程学习 > C#/ASP.NET >>

GridView的多列排序实现

     在GridView里如何实现多条件的排序。
      如:排序列为:省,城市。点击省,排序,然后点击城市,省不变,再按照城市排序。 --------------------编程问答-------------------- 点击后把要排序的列记到变量里
然后根据这些列写sql排序就可以了

比方说有个变量dim m_strOrder as string=""
点省时
if(m_strOrder.Length>0) then
   m_strOrder=m_strOrder & ",";
m_strOrder= m_strOrder & "省字段"

strSql="SELECT * FROM 表 ORDER BY " + m_strOrder;
绑定gridview

点城市时
if(m_strOrder.Length>0) then
   m_strOrder=m_strOrder & ",";
m_strOrder= m_strOrder & "市字段"

strSql="SELECT * FROM 表 ORDER BY " + m_strOrder;
--------------------编程问答-------------------- 帮顶下 --------------------编程问答-------------------- 建议使用 BindingSource ,然后将 BindingSource 作为 DataGridView 的数据源,把 DataTable 作为 BindingSource 的数据源

这样,要怎么排序就怎么排序。如:

BindingSource.Sort = "省,城市"

格式:BindingSource.Sort = "列名1, 列名2, 列名3, ..., 列名n" --------------------编程问答-------------------- gxingmin 在1楼的方法是用vb6的思想套用到vb.net中

而且所见非所得,即每次重新排序后都从数据库中重新读取数据,而每次读取的数据不一定会一样,这样对于财务报告来说是不合理的。而且给对账带来麻烦,即工作不能做为有效工作来进行。

财务报告,查询出来的结果是什么样就应当是什么样,除非使用者人为决定重新查询。否则即使数据库中的数据发生改变,也应当将错就错。这叫工作的连续性和有效性 --------------------编程问答-------------------- 不用 BindingSource,用 DataTable.DefaultView.Sort = "省,城市" 也可以

其中 DataTable 为与 DataGridView 的数据源,不过建议还是使用 BindingSource,因为用 BindingSource 对 DataGridView 的操作会方便许多,比如 DataGridView 当前行,就是 BindingSource 的当前行
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,