GridView那点事之【内置的删除、编辑使用】
来源:互联网 发布:mac exe怎么打开方式 编辑:程序博客网 时间:2024/05/17 07:32
删除提示:在RowDataBound中实现
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除该信息吗?')");
}
}
***********
如图,如何实现编辑、更改、取消或删除呢,在GridView中添加内置的编辑和删除,则在每一行的列都会相应显示。
下面我们来具体实现这些功能,很简单的。我们分别激活对应的事件即可。
(1) //进行删除表格中某一行
protected void LinkButtonDelete_Click(object sender, EventArgs e)//删除
{
GridViewRow gvr = (GridViewRow)((DataControlFieldCell)(((LinkButton)(sender)).Parent)).Parent;
int index = Convert.ToInt32(gvr.Cells[0].Text) - 1;
((DataSet)(ViewState["DS_PRODUCTIONPLAN"])).Tables[0].Rows.RemoveAt(index);
//((DataSet)(ViewState["DS_PRODUCTIONPLAN"])).相当于ds
bind();
}
//编辑表格
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑该项
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消修改
{
GridView1.EditIndex = -1;
bind();
}
//编辑更改保存
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //更新
{
//((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
GridViewRow gvr = (GridViewRow)((DataControlFieldCell)(((LinkButton)(sender)).Parent)).Parent;//获取行号
gvr.Cells[1].Text = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
===============================================================================================================================
protected void GV_file_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// ((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["Num"] = decimal.Parse(((TextBox)GV_file.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim());
((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["File"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["Duty"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["FileNum"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
GV_file.EditIndex = -1;
GV_file.DataSource = ViewState["Gv"];
GV_file.DataBind();
}
**************************************************************************************************************************************************************************
倘若需要更新数据库的数据记录时,我们应该在上面的RowUpdating()中添加相关的SQL语句Update。才能进行修改。如自己定义并调用更新数据函数
public bool UpdateData()
{
///更新数据库记录SQL的实现,这里不再阐述,明白这个实现过程的思路就可以了
}
例如:》》》》》》》》》》》》》》》
protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Entity.TB_UnionOrganization E_TB_UnionOrganization = new Entity.TB_UnionOrganization();
//Entity.TB_UnionOrganization是三层架构中的model类
E_TB_UnionOrganization.Id = Convert.ToDecimal(GV_WSH.Rows[e.RowIndex].Cells[11].Text.Trim());
E_TB_UnionOrganization.WChairman = ((TextBox)GV_WSH.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
E_TB_UnionOrganization.WCochairman = ((TextBox)GV_WSH.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
if (B_EditUnionOrganization.UpdateOrganization(E_TB_UnionOrganization))//调用修改数据库的函数
{ msg.AjaxResponeSrcipt(UpdatePanel1, this.GetType(), "修改成功"); bindGV_WSH(); bindCode(); }
else
msg.AjaxResponeSrcipt(UpdatePanel1, this.GetType(), "修改失败");
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//////////////////////**********内置删除**************************////////////////////////////////
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim());
DataAccessSQL delete = new DataAccessSQL();
delete.deleteValue(ID);//实行删除数据库内容记录
BindData(); //重新绑定
}
************************实行删除数据库内容记录
public void deleteValue(int ID)
{
string strSql = "Data Source=VQJREZV7DVSK2QA;Initial Catalog=gridviewAPP;User ID=sa;Password=admin@123456";
SqlConnection connew = new SqlConnection(strSql);
connew.Open();
StringBuilder strDeletet = new StringBuilder();
strDeletet.Append("delete from userInfo ");
strDeletet.Append(" where Id=" + ID + "");
SqlCommand cmd = new SqlCommand(strDeletet.ToString(),connew);
cmd.ExecuteNonQuery();
connew.Close();
}
***************************//重新绑定
protected void BindData()
{
DataAccessSQL selectData = new DataAccessSQL();
DataSet ds = new DataSet();
ds = selectData.DataAdapter();
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds != null)
{
ds.Tables[0].Columns.Add("gradeName", typeof(string));
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["gradeID"] != DBNull.Value)
{
dr["gradeName"] = selectData.GradeAdapter(Convert.ToInt32(dr["gradeID"].ToString()));
}
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
- GridView那点事之【内置的删除、编辑使用】
- GridView那点事之【删除时弹出确认对话框】
- GridView那点事之【自动编号】
- GridView 的编辑删除
- 关于DOM的那点事之创建、追加、删除
- 自定义GRIDVIEW 之编辑删除
- GridView那点事之【如何使用DataView来绑定数据显示到表格中】
- GridView那点事之【如何使用ExecuteReader()来绑定数据显示到表格中】 .
- GridView的编辑、新增、删除
- GridView的编辑删除代码
- GridView那点事之【隐藏某列字段】
- GridView那点事之【表格背景色透明】
- GridView那点事之【如何循环访问每一行】
- 使用GridView添加,删除,编辑记录
- MySQL那点事之插入、更新与删除数据
- DevExpress XtraGrid如何使用GridView内置方式编辑数据
- gridview 编辑,删除,更新的用法
- GridView基本的编辑,更新,删除操作
- jQuery实现' 返回顶部 ' 和 ' 导航悬浮 '
- 在eclipse中如何输入main(String[] args)中的args
- JavaWeb中的Session、SessionListener、在线人数统计
- 让Android模拟器能访问到本地Server或Web应用的方法
- MySQL图文安装配置
- GridView那点事之【内置的删除、编辑使用】
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- R树——空间索引
- OCP-1Z0-051-V9.02-85题
- java中CharSequence接口
- 用递归思想解决河内塔问题
- hdu 4430——Yukari's Birthday
- 题目:输入数字n,按顺序打印从1到n位十进制数;
- 关于C++对象的成员变量的布局问题