DataGrid常用三种方法:分页,排序,后绑定

当DataGrid数据绑定之后
将数据存入Cache;
dgList.DataSource  =  ds;
                
this .Cache[ " DataSet " =  ds;
                dgList.DataBind();
而后
分页:
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// Handles the PageIndexChanged event of the dgList control.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">The source of the event.</param>
ExpandedBlockEnd.gif        
/// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridPageChangedEventArgs"/> instance containing the event data.</param>

None.gif          private   void  dgList_PageIndexChanged( object  source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int pageIndex = 0;
InBlock.gif            
if(e.NewPageIndex>this.dgList.PageCount-1)
InBlock.gif                pageIndex 
= this.dgList.PageCount-1;
InBlock.gif            
else if(e.NewPageIndex<0)
InBlock.gif                pageIndex 
= 0;
InBlock.gif            
else
InBlock.gif                pageIndex
=e.NewPageIndex;
InBlock.gif            
this.dgList.CurrentPageIndex = pageIndex;
InBlock.gif
InBlock.gif            
this.dgList.DataSource = (DataSet)this.Cache["DataSet"];
InBlock.gif            
this.dgList.DataBind();
ExpandedBlockEnd.gif        }
排序:
注意必须在DataGrid中属性生成器设置排序表达式。
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// Handles the SortCommand event of the dgList control.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">The source of the event.</param>
ExpandedBlockEnd.gif        
/// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridSortCommandEventArgs"/> instance containing the event data.</param>

None.gif          private   void  dgList_SortCommand( object  source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if((string)this.ViewState["Sort"!= e.SortExpression)
InBlock.gif                
this.ViewState["Sort"= e.SortExpression;
InBlock.gif            
else
InBlock.gif                
this.ViewState["Sort"= e.SortExpression+" DESC";
InBlock.gif
InBlock.gif
InBlock.gif            DataSet ds 
= (DataSet)this.Cache["DataSet"];
InBlock.gif            ds.Tables[
0].DefaultView.Sort = (string)this.ViewState["Sort"];
InBlock.gif            
this.dgList.DataSource = ds.Tables[0].DefaultView;
InBlock.gif            
this.dgList.DataBind();
ExpandedBlockEnd.gif        }

绑定 :
第一个IF语句表示鼠标移动,所选行变色。
第二个IF可以在{}内e.Item.Cells[*].Text设置DataGrid的值,也可再次绑定控件。或对绑定的控件加入javascript代码。
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// Handles the ItemDataBound event of the dgList control.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender">The source of the event.</param>
ExpandedBlockEnd.gif        
/// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>

None.gif          private   void  dgList_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif        
InBlock.gif            
if (e.Item.ItemType!=ListItemType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                e.Item.Attributes.Add( 
"onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
InBlock.gif                e.Item.Attributes.Add( 
"onmouseover","this.style.backgroundColor=\"""Gray"+"\"");
ExpandedSubBlockEnd.gif                     }

InBlock.gif            
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif                     }

ExpandedBlockEnd.gif         }
posted on 2007-04-10 11:10 rico 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ricozhao/archive/2007/04/10/706767.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值