以前自学Asp.net的总结(续1)--DataGrid控件

DataGrid分页

在DataGrid控件的PageIndexChange事件里面
==>  this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
     this.DataGrid1.DataBind();

隐藏某列
 this.DataGrid1.Columns[i].Visible=false;
 this.DataGrid1.DataBind();


DataGrid实现鼠标移动特效
==>在ItemDataBound事件里面(这是在当数据绑定到DataGrid控件时发生)

if(e.Item.ItemTpye==ListItemType.Item ||e.ItemType==ListItemType.AlternatingItem)
{
   e.Item.Attributes.Add ("onmouseover","c=this.style.backgroundColor;this.style.back='#6699ff'");

 e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c;");
}


超级链接列,可以实现单击,连接到另一个页面

 

DataGrid排序(在sortCommand事件)
通过一个ViewState对象==>用于记录客户端的排序
1.

if(ViewState["sort"]==null
    ViewState["sort"]="ASC";
else
{
   if(ViewState["sort"].ToString()=="ASC")
 ViewState["sort"]="DESC";
   else
  ViewState["sort"]="ASC"; 
}

2.
通过查询填充一个DataSet
ds.Tables["emp"].DefaultView.Sort=e.SortExpression+" "+ViewState["sort"].ToString();
this.DataGrid1.DataSource=ds.Tables["emp"].DefaultView;
this.DataGrid1.DataBind();

 

3.
在DataGrid里面实现删除操作 ==>在DataGrid的DeleteCommand事件里面
//在绑定时,必须有绑定DataKeyField
//取出主键(用行的索引)
string empID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();


4.
//取出这个控件
((LinkButton)(e.Item.Cell[6].Controls[0]))

//可以在上面的ItemDataBound事件里面给按钮加上事件(插入脚本)
((LinkButton)(e.Item.Cell[6].Controls[0])).Attributes.Add("onclick","return confirm('你确定删除');");


5.编辑,更新,取消 列(直接添加这个列就行了)

//编辑(在EditCommand事件)
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
再重新绑定


//取消(在CancelCommand事件)
this.DataGrid1.EditItemIndex=-1;
再重新绑定


//更新(很少用这种方式来更新)
//取出主键
string empId=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string firstname=((TextBox)(e.Item.Cell[i].Controls[0])).Text;
//依次取出
......
//更新回数据库....
...
//取消更新状态
this.DataGrid1.EditItemIndex=-1;
再重新绑定


遍历DataGrid
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
   CheckBox chk=(CheckBox)dl.FindControl("ID即控件名"); 
   //对查找到的控件进行操作 
   .........
}


  
!注意

(有时候在模板里面加了验证控件,还能更新) ===>
可以将里面的列转换为模板列,在这个更新控件上找到 CausesValidation这个属性,将其高为True  表示是否触发验证


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值