DataGrid Sort

我所使用过的DataGrid 排序方法。不同的代码适用不同的条件,一定要审慎用之啊。
1、
//  #region /***列排序***/
//  public void SortColumn(DataGrid  dataGrid ,int columnIndex)
//  {
//   try
//   {
//    System.ComponentModel.PropertyDescriptor pd =
//     dataGrid.TableStyles[0].GridColumnStyles[columnIndex].PropertyDescriptor;

//  //if the above line of code didn't work try to get a propertydescriptor
//  // via MappingName
  if(pd == null)
 {
  System.ComponentModel.PropertyDescriptorCollection pdc =
   System.ComponentModel.TypeDescriptor.GetProperties(sourceType);
  pd =
  pdc.Find( this.TableStyles[0].GridColumnStyles[columnIndex].MappingName,
  false);
 }
//    //now invoke ColumnHeaderClicked method using system.reflection tools
//    System.Reflection.MethodInfo mi =
//     typeof(System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked",
//     System.Reflection.BindingFlags.Instance |
//     System.Reflection.BindingFlags.NonPublic);
//    mi.Invoke(dataGrid, new object[] {pd});
//   }
//   catch (Exception err)
//   {
//    MessageBox.Show(this,err.Message.ToString(),"列排序",MessageBoxButtons.OK,MessageBoxIcon.Information); 
//   }
//  }
//
//  #endregion

2、
//  #region /***列排序***/
//  public void SortDataGrid(object sender, System.Windows.Forms.MouseEventArgs e)
//  {
//   DataGrid.HitTestInfo hitTest;
//   DataTable dataTable;
//   DataView dataView;
//   string columnName;
//   DataGrid dataGrid;
//
//   // Use only left mouse button clicks.
//   if (e.Button == MouseButtons.Left)
//   {
//    dataGrid = (DataGrid)sender;
//    hitTest = dataGrid.HitTest(e.X, e.Y);
//    if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
//    {
//     DataSet ds=(DataSet)dataGrid.DataSource;
//     dataTable =ds.Tables[0];
//     dataView= dataTable.DefaultView;
//  
//     if(dataGrid.TableStyles.Count != 0)
//      columnName = dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
//     else
//      columnName = dataTable.Columns[hitTest.Column].ColumnName;
//
//     if (dataView.Sort == columnName)
//      dataView.Sort = columnName + " DESC";
//     else
//      dataView.Sort = columnName;
//    }
//   }
//  }
//
//  #endregion
//
//  private void dgDataGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
//  {
//   if(dgDataGrid.VisibleRowCount == 0) return;
//   SortDataGrid(sender, e);
//   dgDataGrid.Select(dgDataGrid.CurrentRowIndex);
//  }
//

转载于:https://www.cnblogs.com/flashicp/archive/2007/06/05/771549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值