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 表示是否触发验证