利用模板列实现DataGrid控件的双向排序

    初用DataGrid控件,想实现点击表头来对数据进行双向排序,费了老半天工夫才摸索出如下的东东来,请看效果图:  
   
33.gif

DataGrid设置如下:

None.gif < asp:DataGrid  id ="faggruppeGrid"  runat ="server"  BorderColor ="Black"  BorderWidth ="1px"  CellPadding ="3"
None.gif    Width
="300px"  HeaderStyle-BackColor ="#7f9db9"  HorizontalAlign ="Center"  AutoGenerateColumns ="False"
None.gif    AllowSorting
="True" >
None.gif    
< HeaderStyle  BackColor ="#7F9DB9" ></ HeaderStyle >
None.gif    
< Columns >
None.gif    
<!--  第一列  -->
None.gif     
< asp:TemplateColumn >
None.gif      
< HeaderTemplate >
None.gif       
< asp:LinkButton  id ="lbFag"  runat ="server"  CommandName ="sort"  CommandArgument ="Fag"  CausesValidation ="False" > Fag </ asp:LinkButton >
None.gif       
< asp:Image  Runat ="server"  ID ="ImgFag"  ImageUrl ="../images/Up.gif"  Visible ="False"   />      <!-- 箭头图标 -->
None.gif      
</ HeaderTemplate >
None.gif      
< ItemTemplate >
None.gif       
< asp:Label  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.Fag") % > ' ID="Label1"/>
None.gif      
</ ItemTemplate >
None.gif     
</ asp:TemplateColumn >
None.gif    
<!--  第二列  -->
None.gif     
< asp:TemplateColumn >
None.gif      
< HeaderTemplate >
None.gif       
< asp:LinkButton  id ="lbNavn"  runat ="server"  CommandName ="sort"  CommandArgument ="Navn"  CausesValidation ="False" > Navn </ asp:LinkButton >
None.gif       
< asp:Image  Runat ="server"  ID ="ImgNavn"  ImageUrl ="../images/Up.gif"  Visible ="False"   />
None.gif      
</ HeaderTemplate >
None.gif      
< ItemTemplate >
None.gif       
< asp:Label  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.Navn") % > ' ID="Label2"/>
None.gif      
</ ItemTemplate >
None.gif     
</ asp:TemplateColumn >
None.gif    
<!--  第三列  -->
None.gif     
< asp:HyperLinkColumn  DataNavigateUrlField ="Fag"  DataNavigateUrlFormatString ="JavaScript:ReturnFagnummer('{0}');"  Text ="Indsæt"   />
None.gif    
</ Columns >
None.gif   
</ asp:DataGrid >
None.gif



后台处理代码:

 

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  Pensam.Praesentation.BMSWIService;
None.gif
using  Pensam.GenNS;
None.gif
using  Microsoft.ApplicationBlocks.ExceptionManagement;
None.gif
None.gif
namespace  Pensam.Praesentation.Medarbejder
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for FaggruppeListe.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FaggruppeListe : BaseWebPage
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
member variables#region member variables
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid faggruppeGrid;
InBlock.gif        
protected System.Web.UI.WebControls.Image ImgArrow = null;
InBlock.gif        
private DataTable dt = null;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            VisFagGrps();
InBlock.gif            
if(!this.IsPostBack) BindGrid();
InBlock.gif            
//if(this.IsPostBack) Response.Write("post back");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.faggruppeGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.faggruppeGrid_SortCommand);
InBlock.gif            
this.faggruppeGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.faggruppeGrid_ItemDataBound);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private functions#region private functions
InBlock.gif        
//生成绑定数据
InBlock.gif
        private void VisFagGrps()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*
InBlock.gif            using(PSLog logger = new PSLog())
InBlock.gif            {
InBlock.gif                try
InBlock.gif                {
InBlock.gif                    if(this.ServiceDelegate == null) return;
InBlock.gif                    ArrayList list1 = new ArrayList(this.ServiceDelegate.GetFaggruppeListe());
InBlock.gif                    if(list1 == null)
InBlock.gif                    {
InBlock.gif                        this.VisErrorMessage("Get professional category list failed.");
InBlock.gif                    }
InBlock.gif                }
InBlock.gif                catch(Exception ex)
InBlock.gif                {
InBlock.gif                    VisErrorMessage(ex.Message);
InBlock.gif                    ExceptionManager.Publish(ex);
InBlock.gif                }
InBlock.gif            }
ExpandedSubBlockEnd.gif            
*/

InBlock.gif            
InBlock.gif
InBlock.gif            dt 
= new DataTable();
InBlock.gif            DataRow dr;
InBlock.gif
InBlock.gif            dt.Columns.Add(
new DataColumn("Fag"typeof(string)));
InBlock.gif            dt.Columns.Add(
new DataColumn("Navn"typeof(string)));
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag01000001";
InBlock.gif            dr[
"Navn"= "Fag0101";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag01000002";
InBlock.gif            dr[
"Navn"= "Fag0102";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag01000003";
InBlock.gif            dr[
"Navn"= "Fag0103";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag02000001";
InBlock.gif            dr[
"Navn"= "Fag0201";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag02000002";
InBlock.gif            dr[
"Navn"= "Fag0202";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag02000003";
InBlock.gif            dr[
"Navn"= "Fag0203";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag03000001";
InBlock.gif            dr[
"Navn"= "Fag0301";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag03000002";
InBlock.gif            dr[
"Navn"= "Fag0302";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            dr 
= dt.NewRow();
InBlock.gif            dr[
"Fag"= "Fag03000003";
InBlock.gif            dr[
"Navn"= "Fag0303";
InBlock.gif            dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void faggruppeGrid_SortCommand(Object sender, DataGridSortCommandEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Web.UI.Control control 
= ((DataGridItem)e.CommandSource).FindControl("ImgArrow");
InBlock.gif            
//Response.Write(control.ToString());
InBlock.gif
            ImgArrow = (System.Web.UI.WebControls.Image)control;
InBlock.gif            
string SortField = (string)e.SortExpression;
InBlock.gif            
string oldSortParam = faggruppeGrid.Attributes["SortExpression"];
InBlock.gif            
if(oldSortParam.StartsWith(SortField))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//order by the same column.
InBlock.gif
                 if(oldSortParam.EndsWith("ASC")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                         oldSortParam 
= oldSortParam.Replace("ASC","DESC");
InBlock.gif                         
//ImgArrow.ImageUrl = "../Images/Up.gif"; //实践证明在此设置图像行不通,需在ItemDataBound事件中实现。
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        oldSortParam 
= oldSortParam.Replace("DESC","ASC");
InBlock.gif                        
//ImgArrow.ImageUrl = "../Images/Up.gif"; //实践证明在此设置图像行不通,需在ItemDataBound事件中实现。
ExpandedSubBlockEnd.gif
                    }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                oldSortParam 
= SortField + " ASC";
ExpandedSubBlockEnd.gif            }

InBlock.gif            faggruppeGrid.Attributes[
"SortExpression"= oldSortParam;
InBlock.gif            BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//绑定数据
InBlock.gif
        private void BindGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//设置初始排序字段
InBlock.gif
            if(faggruppeGrid.Attributes["SortExpression"== null)
InBlock.gif                faggruppeGrid.Attributes[
"SortExpression"= "Fag ASC";
InBlock.gif            DataView dv 
= dt.DefaultView;
InBlock.gif            
//按指定的排序字段排序
InBlock.gif
            dv.Sort = faggruppeGrid.Attributes["SortExpression"];
InBlock.gif            faggruppeGrid.DataSource 
= dt;
InBlock.gif            faggruppeGrid.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void faggruppeGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType != ListItemType.Header) return;
InBlock.gif            
//以下获取排序表达式
InBlock.gif
            if(faggruppeGrid.Attributes["SortExpression"]==nullreturn;
InBlock.gif            
string sortExpression = faggruppeGrid.Attributes["SortExpression"];
InBlock.gif            
string[] orderParams = sortExpression.Split(' ');
InBlock.gif             
//获取Image对象
InBlock.gif
            System.Web.UI.Control control = (e.Item).FindControl("Img" + orderParams[0]);
InBlock.gif
InBlock.gif            
if(control == nullreturn;
InBlock.gif            ImgArrow 
= (System.Web.UI.WebControls.Image)control;
InBlock.gif            ImgArrow.Visible 
= true;
InBlock.gif
InBlock.gif            
//设置图像Url
InBlock.gif
            if(sortExpression.EndsWith("ASC")) 
InBlock.gif                ImgArrow.ImageUrl 
= "../Images/Up.gif";
InBlock.gif            
else
InBlock.gif                ImgArrow.ImageUrl 
= "../Images/Down.gif";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/zengdj/archive/2005/07/01/184564.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值