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

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 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。

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