惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - YiStudio

在Word中插入图片 将Word嵌入到自己的程序中 一个非常不错的压缩组件 免费的国产报表Grid++Reprot 使用NSIS制作安装包(2) 使用NSIS制作安装包(1) SharpDevelop2.0 用Firebird .NET Data Provider编写.NET应用程序(2) 用Marathon管理Firebird数据库(2) 用Firebird .NET Data Provider编写.NET应用程序(1) 用Marathon管理Firebird数据库(1) Firebird简介 NDoc修改手记(三) NDoc修改手记(二) NDoc修改手记(一) WebForm中将DataGrid中导出数据的方法 FxCop EXCEL2003中使用XML 动态加载类(动态加载DLL文件)
DataGridView多列排序
YiStudio · 2009-12-10 · via 博客园 - YiStudio

    最近写的一个程序中需要用到DataGridView的多列排序,参考了一下MSDN上的东西,似乎只能用于两列而且不是很灵活(也许是我没有真正弄明白),由于时间的关系,没有仔细去研究。想到了下面这个方法:

代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Windows.Forms;
  5 
  6 namespace DataGridViewColumnSort
  7 {
  8     /// <summary>
  9     /// Description of MainForm.
 10     /// </summary>
 11     public partial class MainForm : Form
 12     {
 13         public MainForm()
 14         {
 15             //
 16             // The InitializeComponent() call is required for Windows Forms designer support.
 17             //
 18             InitializeComponent();
 19             
 20             //
 21             // TODO: Add constructor code after the InitializeComponent() call.
 22             //
 23             this.BuildSortInfoTable();
 24             this.FillDataGridView();
 25             this.SetColumnSortMode();
 26         }
 27         
 28         private System.Data.DataTable dtSort;
 29         private System.Data.DataView dv;
 30         
 31         private void BuildSortInfoTable()
 32         {
 33             dtSort=new System.Data.DataTable();
 34             dtSort.Columns.Add("Field",System.Type.GetType("System.String"));
 35             dtSort.Columns.Add("Sort",System.Type.GetType("System.String"));
 36             dtSort.Columns.Add("ColumnText",System.Type.GetType("System.String"));
 37             dtSort.Columns.Add("ColumnIndex",System.Type.GetType("System.String"));
 38         }
 39         
 40         private void FillDataGridView()
 41         {
 42             System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection();
 43             cn.ConnectionString="Server=.;User ID=sa;Password=;Database=Northwind";
 44             string strSQL="select LastName,FirstName,Title,BirthDate,Address,City from Employees";
 45             System.Data.SqlClient.SqlDataAdapter ad=new System.Data.SqlClient.SqlDataAdapter(strSQL,cn);
 46             System.Data.DataSet ds=new System.Data.DataSet();
 47             ad.Fill(ds);
 48             dv=ds.Tables[0].DefaultView;
 49             this.dataGridView1.DataSource=dv;
 50         }
 51         
 52         private void SetColumnSortMode()
 53         {
 54             for(int i=0;i<this.dataGridView1.Columns.Count;i++)
 55             {
 56                 this.dataGridView1.Columns[i].SortMode= DataGridViewColumnSortMode.Programmatic;
 57             }
 58         }
 59         
 60         void DataGridView1ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
 61         {
 62             for(int i=0;i<this.dataGridView1.Columns.Count;i++)
 63             {
 64                 this.dataGridView1.Columns[i].ToolTipText=null;
 65             }
 66             string strField=this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
 67             if(dtSort.Rows.Count==0)
 68             {
 69                 System.Data.DataRow dr=dtSort.NewRow();
 70                 dr[0]=strField;
 71                 dr[1]="ASC";
 72                 dr[2]=this.dataGridView1.Columns[e.ColumnIndex].HeaderText;
 73                 dr[3]=e.ColumnIndex;
 74                 dtSort.Rows.Add(dr);
 75             }
 76             else
 77             {
 78                 if(dtSort.Select("Field='"+strField+"'").Length==0)
 79                 {
 80                     System.Data.DataRow dr=dtSort.NewRow();
 81                     dr[0]=strField;
 82                     dr[1]="ASC";
 83                     dr[2]=this.dataGridView1.Columns[e.ColumnIndex].HeaderText;
 84                     dr[3]=e.ColumnIndex;
 85                     dtSort.Rows.Add(dr);
 86                 }
 87                 else
 88                 {
 89                     for(int i=0;i<dtSort.Rows.Count;i++)
 90                     {
 91                         if(dtSort.Rows[i][0].ToString()==strField)
 92                         {
 93                             if(dtSort.Rows[i][1].ToString()=="ASC")
 94                             {
 95                                 dtSort.Rows[i][1]="DESC";
 96                                 break;
 97                             }
 98                             else
 99                             {
100                                 dtSort.Rows.RemoveAt(i);
101                             }
102                         }
103                     }
104                 }
105             }
106             dv.Sort=null;
107             for(int i=0;i<dtSort.Rows.Count;i++)
108             {
109                 if(i==0)
110                     dv.Sort=dtSort.Rows[i][0].ToString()+" "+dtSort.Rows[i][1].ToString();
111                 else
112                     dv.Sort+=","+dtSort.Rows[i][0].ToString()+" "+dtSort.Rows[i][1].ToString();
113                 int iIndex=Convert.ToInt32(dtSort.Rows[i][3]);
114                 if(dtSort.Rows[i][1].ToString()=="ASC")
115                     this.dataGridView1.Columns[iIndex].ToolTipText=dtSort.Rows[i][2].ToString()+" 升序 "+(i+1).ToString();
116                 else
117                     this.dataGridView1.Columns[iIndex].ToolTipText=dtSort.Rows[i][2].ToString()+" 降序 "+(i+1).ToString();
118             }
119         }
120     }
121 }
122 

     大致的思路就是利用DataGridView绑定数据源DataView对象的Sort方法来实现,还是通过点击DataGridView的列头来实现。为了显示列排序的方式和顺序使用了ToolTip。

    方法还有缺陷有待改进,先贴出来和大家分享。