当删除列在最后一列时,给删除前加上确认
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,那我也没有办法),所以,每个人可能都有自己的领悟,这篇文章,算是抛砖引玉,为大家做个铺垫。
一、方法
1、DataBind
很简单、最常用的方法。绑定数据用。需要注意的只有一点:执行了这个方法后,DataGrid(由于DataGrid和DataList极为相似,所以下面的介绍虽然是针对DataGrid,但与DataList也相差不远)里面所有的显示绑定数据的控件,都会显示DataSource里的数据,其余控件也将初始化成.aspx里设计的状态。
二、属性
1、DataSource
有DataBind的地方,就应该有DataSource。如果没有指定DataSource而执行DataBind,那DataGrid将什么也不会显示。
DataSource一般是DataSet、DataTable或者DataView。当然也可以绑定DataReader或者其他实现IEnumerable的类。
2、DataKeyField,DataKeys
当你在DataGrid中定位一行之后,肯定想知道这行在数据表里的位置,至少有五种方法可以做到这一点