ASP.NET - GridView实现点击编辑列

时间:2021-10-23 20:43:38

加载:

ASP.NET - GridView实现点击编辑列

点击编辑:

ASP.NET - GridView实现点击编辑列

数据库设计:

ASP.NET - GridView实现点击编辑列

前端代码:

  1. DataKeyNames="ID"  设置点击“编辑”选项的时候,要获取的值,一般获取ID主键,便于修改数据。
  2. AutoGenerateColumns="False"  设置“设置是否自动生成列”为False。
  3. ReadOnly="True"  设置为只读,这样在点击编辑的时候,设置为只读的字段,不会变成文本框。
  4. DataField="ID"  设置绑定数据的字段名称。
  5. OnRowEditing ="GridView1_RowEditing"  编辑。
  6. OnRowDeleting ="GridView1_RowDeleting"  删除。
  7. OnRowCancelingEdit ="GridView1_RowCancelingEdit"  取消。
  8. OnRowUpdating ="GridView1_RowUpdating1">  更新。
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
DataKeyNames="ID"
AutoGenerateColumns="False"
OnRowEditing ="GridView1_RowEditing"
OnRowDeleting ="GridView1_RowDeleting"
OnRowCancelingEdit ="GridView1_RowCancelingEdit"
OnRowUpdating ="GridView1_RowUpdating1">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="True" />
<asp:BoundField HeaderText="Name" DataField="Name" ReadOnly="True" />
<asp:BoundField HeaderText="Score" DataField="Score"/>
<asp:CommandField HeaderText ="删除" ShowDeleteButton="true" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>

后台代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSoure();
}
} /// <summary>
/// 绑定数据源
/// </summary>
public void BindSoure()
{
this.GridView1.DataSource = SQLHelper.ExecuteTable("select ID, Name, Score from Tb_Mark", System.Data.CommandType.Text);
this.GridView1.DataBind();
} /// <summary>
/// 编辑记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//获得编辑列
GridView1.EditIndex = e.NewEditIndex;
//重新绑定数据
BindSoure();
} /// <summary>
/// 删除记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//获取编辑列的键值对中的值。在属性的DataKeyNames中设置。对一个的ID
int UpdateIdentifier = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); string text = "delete from Tb_Mark where ID = '" + UpdateIdentifier + "'"; if (SQLHelper.ExecuteNonQuery(text, System.Data.CommandType.Text) != 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('删除失败!')</script>");
}
//重新绑定数据
BindSoure();
} /// <summary>
/// 取消修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//退出编辑状态
GridView1.EditIndex = -1;
//重新绑定数据
BindSoure();
} /// <summary>
/// 更新修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
//获取编辑列的键值对中的值。在属性的DataKeyNames中设置。对一个的ID
int UpdateIdentifier = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
//获取第三列Score的值(从0开始),要修改的值。
int UpdateValue = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString()); string text = "update Tb_Mark set Score = '" + UpdateValue + "' where ID = '" + UpdateIdentifier + "'"; if (SQLHelper.ExecuteNonQuery(text, System.Data.CommandType.Text) != 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('更新失败!')</script>");
}
//退出编辑状态
GridView1.EditIndex = -1;
//重新绑定数据
BindSoure();
}