載入:
點選編輯:
資料庫設計:
前端程式碼:
- DataKeyNames="ID" 設定點選“編輯”選項的時候,要獲取的值,一般獲取ID主鍵,便於修改資料。
- AutoGenerateColumns="False" 設定“設定是否自動生成列”為False。
- ReadOnly="True" 設定為只讀,這樣在點選編輯的時候,設定為只讀的欄位,不會變成文字框。
- DataField="ID" 設定繫結資料的欄位名稱。
- OnRowEditing ="GridView1_RowEditing" 編輯。
- OnRowDeleting ="GridView1_RowDeleting" 刪除。
- OnRowCancelingEdit ="GridView1_RowCancelingEdit" 取消。
- 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();
}