DataGridView多列排序

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

ExpandedBlockStart.gif 代码
  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。

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

转载于:https://www.cnblogs.com/yistudio/archive/2009/12/10/1621312.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值