標籤:new prot localhost 綁定 tab password com 自己 text
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; Bindgrid(); }//編輯按鍵下的取消代碼。將EditIndex=-1,然後在綁定資料庫。 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id = GridView1.DataKeys[e.RowIndex][0].ToString(); DeleteGridView(id); Bindgrid(); }// id = GridView1.DataKeys[e.RowIndex][0].ToString();獲得girdwiew中表的主鍵,作為刪除資料的標識。DeleteGirdView()為自己寫的刪除函數 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; Bindgrid(); }//擷取當前編輯狀態 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string id = GridView1.DataKeys[e.RowIndex][0].ToString(); string uid = ((TextBox )GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; string upassword = ((TextBox )GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; string upower = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; UpdataGridView(id, uid, upassword, upower); GridView1.EditIndex = -1; Bindgrid(); }//獲得GridView中選中行的各列資料,用UpdataGridView()更新資料 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; Bindgrid(); }//換頁的時候擷取當前編輯頁 public void UpdataGridView(string id,string uid,string upassword,string upower) { SqlConnection con=new SqlConnection ("server=localhost;database=YGXXXT;uid=sa;password=00"); string sql="update Users set ID=‘"+id+"‘,UID=‘"+uid+"‘,UPassword=‘"+upassword+"‘,UPower=‘"+upower+"‘"; SqlCommand cmd=new SqlCommand (sql,con ); con.Open (); cmd.ExecuteNonQuery (); con.Close(); }//更新資料庫資料,注意sql語句裡的‘"+id+‘"不要弄錯格式~~~ public void DeleteGridView(string id) { SqlConnection con = new SqlConnection("server=localhost;database=YGXXXT;uid=sa;password=00"); string sql = "delete Users where ID=‘"+id +"‘"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); }//刪除資料庫資料
asp.net-gridview