zoukankan      html  css  js  c++  java
  • silverlight datagrid控件,单击标题自定义排序

    直接贴代码

    <sdk:DataGrid AutoGenerateColumns="False" CanUserSortColumns="False" CanUserReorderColumns="False"  IsReadOnly="True" HorizontalAlignment="Left" Margin="0,0,0,86" x:Name="ErrorCodeGrid" Width="662" Grid.Row="1" MouseLeftButtonDown="ErrorCodeGrid_MouseLeftButtonDown">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="110"/>
            <sdk:DataGridTextColumn Header="地市" Binding="{Binding Area}" Width="110"/>
            <sdk:DataGridTextColumn Header="发送/接收" Binding="{Binding Pattern}" Width="95" />
            <sdk:DataGridTextColumn Header="短信类型" Binding="{Binding EmailType}" Width="115"/>
            <sdk:DataGridTextColumn Header="机型" Binding="{Binding PhoneType}" Width="115"/>
            <sdk:DataGridTextColumn Header="比例" Binding="{Binding Ratio}" Width="*"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

    //下面为单机事件

    private void ErrorCodeGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var u = from element in VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), ErrorCodeGrid)
                where element is DataGridColumnHeader
                select element;
        if (u.Count() == 1)
        {
            //鼠标点击的ColumnHeader
            DataGridColumnHeader header = (DataGridColumnHeader)u.Single();
            //要排序的字段
            string _newSort = header.Content.ToString();
            if (dir == "desc")
            {
                dir = "asc";
            }
            else
            {
                dir = "desc";
            }
            sortFiled = _newSort;
            sortHeader = header;
            SortData(dir, sortFiled);
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
    }

    //排序事件

    void SortData(string sortType, string sortFiled)
    {
        dDetailList = ChangeResult(DetailList);
        if (sortType == "desc")
        {
            switch (sortFiled)
            {
                case "比例":
                    dDetailList = dDetailList.OrderByDescending(u => u.Ratio).ToList();
                    break;
     
                default:
                    break;
            }
        }
        else
        {
            switch (sortFiled)
            {
                case "比例":
                    dDetailList = dDetailList.OrderBy(u => u.Ratio).ToList();
                    break;
                default:
                    break;
            }
        }
        DetailList = ChangeBackResult(dDetailList);
        fenye("first");
    }

    //分页事件

    void fenye(string done)
           {
               TotalCount = DetailList.Count();
               if (TotalCount % PageSize == 0)
               {
                   TotalPage = TotalCount / PageSize;
               }
               else
               {
                   TotalPage = TotalCount / PageSize + 1;
               }

               first.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
               pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
               next.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
               last.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));

               if (done == "first")
               {
                   CurrentPage = 1;
               }
               else if (done == "last")
               {
                   CurrentPage = TotalPage;
                   next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               }
               else if (done == "pre")
               {
                   if (CurrentPage > 1)
                       CurrentPage -= 1;
                   else
                   {
                       first.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                       pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   }
               }
               else if (done == "next")
               {
                   if (CurrentPage < TotalPage)
                       CurrentPage += 1;
                   else
                   {
                       next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                       last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   }
               }

               if (TotalPage == 1)
               {
                   first.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               }

               pageShow.Text = CurrentPage + "/" + TotalPage;
               List<ErrorCodeDetailBind> query = DetailList.Take(CurrentPage * PageSize).Skip((CurrentPage - 1) * PageSize).ToList();
               ErrorCodeGrid.ItemsSource = query;
           }

  • 相关阅读:
    Memcached 笔记与总结(5)Memcached 的普通哈希分布和一致性哈希分布
    大数据价值挖掘:聚焦商业模式探索
    微博商业数据挖掘方法
    微博商业数据挖掘方法
    如何用大数据探索市场营销实践
    如何用大数据探索市场营销实践
    数据挖掘基本任务
    数据挖掘基本任务
    医疗大数据分析深入浅出
    医疗大数据分析深入浅出
  • 原文地址:https://www.cnblogs.com/honkcal/p/2353086.html
Copyright © 2011-2022 走看看