菜鸟请教一Gridview问题!!!
Gridview绑定数据后怎么样实现数据的编辑和删除功能!
也就是说编辑中的更新和取消,还有删除这三个事件怎么写?
希望各位大虾能说的详细一点,更给出详细的代码更好,谢谢!!!
------解决方案--------------------SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=db_04;Uid=jiang;Pwd=123 ";
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 tb_Member 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 tb_Member set name= ' "
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + " ',sex= ' "
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + " ',nPlace= ' "
+ ((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 tb_Member ";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member ");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id " };
GridView1.DataBind();
sqlcon.Close();
}
------解决方案--------------------后台
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div align= "left ">
<table width= "100% ">
<tr>
<td style= "width: 56px; height: 237px; ">
</td>
<td align= "left " style= "height: 237px ">
<asp:GridView ID= "GridView1 " AllowSorting= "True " AllowPaging= "True " PageSize= "3 "
AutoGenerateColumns= "False " runat= "server " On