GridView那点事之【内置的删除、编辑使用】
2013-10-18 21:47
288 查看
删除提示:在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();
}
}
相关文章推荐
- 【Vegas原创】VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- GridView行编辑、更新、取消、删除事件使用方法
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- 【英】使用DLINQ实现GridView的编辑、更新、删除、和分页
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- DevExpress XtraGrid如何使用GridView内置方式编辑数据
- GridView控件编辑、更新、删除示例(更新包含有DropDownList控件使用)
- 【Vegas原创】VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
- VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
- VB.NET版的GridView经典使用(编辑,删除,分页,鼠标行颜色变化、链接列)
- 不使用GridView自带编辑删除等等按钮,放置服务器控件的按钮。获取点击按钮的当前行以便进行编辑删除
- 使用GridView加DetailsView实现查询,新增,编辑,删除
- GridView控件编辑、更新、删除示例(更新包含有DropDownList控件使用)、导出Excel
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- 使用GridView加DetailsView实现查询,新增,编辑,删除
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- 使用gridview实现编辑、更新、取消功能和删除功能
- GridView控件编辑、更新、删除示例(更新包含有DropDownList控件使用)
- GridView控件编辑、更新、删除示例(更新包含有DropDownList控件使用)