DataGridView多列排序

    最近写的一个程序中需要用到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。

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

你可能感兴趣的:(datagridview)