日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

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);
//  }
//

posted on 2007-06-05 09:18  flashicp  阅读(820)  评论(0编辑  收藏  举报

导航