我有一个gridview通过模板绑定数据库。现在我不想通过模板自带的删除和更新的button的字段按钮。我想自建二个button来实现删除和更新gridview。即一个gridview和二个button(删除、更新),点击二个button(删除、更新)就可以实现gridview的删除、更新,并有(要有删除、更新)的确认
解决方案 »
- webBrowser开发自动填表遇到个棘手的问题
- 特别迷茫,请大家帮忙,进来也有分
- 多语言开发最佳实践,让软件的界面支持多国语言,怎么样做才最好?
- [请教][难度问题]按钮事件中有循环的问题,用另一个按钮控制停止,暂停,继续
- 在treeview中,如何把用户用鼠标点击的节点的KEY值拿出来?谢谢
- 技术平台准备迁徙到Dot Net, 请推荐基本Dot Net, C#的书籍。
- Application.StartupPath 在NET WEB里需要引用什么空间名?
- winform导出excel默认保存文件名怎么改?
- 名为“序列号”的列已属于此 DataTable!!!!!!
- 编译出错.大家帮帮忙.Error CS0212
- 小弟联系想帮人做一个项目,不知道开价多少较好,各位给个意见?
- 版主进来看啊,这个有个very very very Big bug
给个连接:http://www.cnblogs.com/aspnetx/archive/2006/08/07/470341.html
----------------
|
gridview |
|
|
---------------|
删除(按钮) 更新(按钮)
为删除按钮添加javascript事件 onclientclick=return window.confirm("确实要删除本信息吗?");
执行起来也是有点复杂了
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}//绑定
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
}
1 aa tt |
2 bb yy |
3 cc uu |
4 dd ii | gridview
-----------------------
序号___ (textbox)
姓名___ (textbox)
单位___ (textbox)更新 删除
选定一行后自动填充在下方的textbox中序号、姓名、单位中,点删除按钮后gridview就删除一行。
private static IList<ProductModel> lists=new ProductBll(Program.factory);this.dataGridView1.DataSource = lists;删除:
ProductModel pm = (ProductModel)lists[this.dataGridView1.CurrentRow.Index];
lists.Remov(pm);
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = lists;
1 aa tt |
2 bb yy |
3 cc uu |
4 dd ii |gridview
-----------------------
序号___ (textbox)
姓名___ (textbox)
单位___ (textbox)更新 删除
选定一行后自动填充在下方的textbox中序号、姓名、单位中,点删除按钮后gridview就删除一行。
各位高手,前面我没写清楚。请各位赐教。
建一个模板列,里面放两个Button,然后起上不同的CommandName
然后一个RowCommand事件里加入啦
http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.