当删除列在最后一列时,给删除前加上确认
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int t=e.Item.Cells.Count-1;
LinkButton lbt=(LinkButton)(e.Item.Cells[t].Controls[0]);
lbt.Attributes.Add("onclick","return confirm('删除后将无法恢复,是否继续?')");
}
使某一行进入编辑状态
dataGrid.EditItemIndex=(int)e.Item.ItemIndex;
this.CreateData();//邦定数据到datagrid的函数
取消编辑状态
dataGrid.EditItemIndex=-1;
this.CreateData();//邦定数据到datagrid的函数
删除函数
string id=BlockGrid.DataKeys[e.Item.ItemIndex].ToString();
string sql=string.Format("delete from bbs_forum where forumid="+id);
iBase=iAdapter.AdapteriDataBase();
iBase.ExecuteNonSql(sql);
Message.Show(this,"删除成功!");
this.CreateData();//邦定数据到datagrid的函数
更新函数
string priority=((TextBox)e.Item.Cells[0].Controls[0]).Text.ToString();
string id=BlockGrid.DataKeys[(int)(e.Item.ItemIndex)].ToString();
string title=((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
string description=((TextBox)e.Item.Cells[2].Controls[0]).Text.ToString();
string categoryid=((TextBox)e.Item.Cells[3].Controls[0]).Text.ToString();
string onlymember=((TextBox)e.Item.Cells[4].Controls[0]).Text.ToString();
string admin=((TextBox)e.Item.Cells[5].Controls[0]).Text.ToString();
sql=string.Format("update bbs_forum set priority={0},title='{1}',description='{2}',categoryid={3},onlymember={4},admin='{5}' where forumid={6}",priority,title,description,categoryid,onlymember,admin,id);
iBase=iAdapter.AdapteriDataBase();
try
{
iBase.ExecuteNonSql(sql);
Message.Show(this,"更新成功");
// Response.Write(sql);
}
catch
{
Message.Show(this,"更新失败,请确认填写信息格式否正确!");
// Response.Write(sql);
}
BlockGrid.EditItemIndex=-1;
this.BindDataGrid(MainBlockList.SelectedValue.ToString());
分页
dateGrid.CurrentPageIndex=e.NewPageIndex;
this.CreateData();
以下内容转载 速马 个人blog
很久以前就想写一些关于DataGrid/DataList的东西,但是一直以来,一方面自感所学未深,另一方面,总觉无从下笔,一拖再拖,离刚开始的念头已距一年有余。
DataGrid/DataList在ASP.NET中的重要性,想必就不用我再强调了,凡显示Table类型的数据,大多会使用这两个控件(当然,如果谁还像ASP那样写ASP.NET,那我也没有办法),所以,每个人可能都

本文详细介绍了ASP.NET中DataGrid控件的常用操作,包括删除确认、进入编辑状态、取消编辑、数据删除与更新,以及分页处理。重点讲解了DataGrid的事件处理,如ItemCommand、PageIndexChanged、ItemDataBound和ItemCreated,并提供了代码示例,如设置编辑框宽度、处理DropDownList事件以及获取Header、Footer、Pager中的控件。文章旨在帮助开发者更好地理解和使用DataGrid。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



