zoukankan      html  css  js  c++  java
  • 一个据说含DataGrid Sort功能的代码。

    http://www.codeproject.com/csharp/datagridsort.asp

    public class MyDataGrid : DataGrid
    {
     
    //sort a column by its index
     public void SortColumn(int columnIndex)
     
    {
      
    if(this.DataSource!=null && 
        ((System.Collections.IList)
    this.DataSource).Count>0)
      
    {
       
    //discover the TYPE of underlying objects
       Type sourceType = ((System.Collections.IList)this.DataSource)[0].GetType();

       
    //get the PropertyDescriptor for a sorted column
       
    //assume TableStyles[0] is used for our datagrid (change it if necessary)
       System.ComponentModel.PropertyDescriptor pd =
        
    this.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(
    thisnew object[] { pd });
      }

     }

    }

    以上代码,我一开始用的时候还可以排序,但是后来发现,鼠标点一下后,必须移动到别的地方,才能看到排序效果。不知道是不是新加了MouseDown引起的。
    于是到处查找不同方法的排序代码。戏剧性的是,测试的时候,我把所有的排序功能代码都屏蔽了。这时竟然DataGrid完全可以排序。狂哭啊,狂哭。早知道这样,我何必到处查找排序方法啊。  哎……
  • 相关阅读:
    asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像
    C#中DataTable中的Compute方法使用收集
    c#的DateTime.Now函数详解
    附加数据库失败,拒绝访问
    xml文件绑定chenckbox选择框
    Maximum Xor Secondary(单调栈好题)
    Y(类树形DP)
    Average distance(类树形DP)
    Balls and Boxes (模拟题)
    Party at Hali-Bula(树形DP+判断方案数是否唯一)
  • 原文地址:https://www.cnblogs.com/flashicp/p/771550.html
Copyright © 2011-2022 走看看