| sp; sqlAdatper.Update(table); con.Close(); this.DataGrid1.EditItemIndex=-1; this.BindGrid(); } /// <summary> /// 点击删除时触发该事件 /// </summary> /// <param name="source"></param> /// <param name="e"></param> private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlConnection con=new SqlConnection("server=accp-lzh;uid=sa;pwd=sasa;database=pubs"); SqlCommand selectCmd=con.CreateCommand(); selectCmd.CommandText="select * from publishers where pub_id like ''''99%''''"; SqlCommand deleteCmd=con.CreateCommand(); deleteCmd.CommandText="delete from publishers where pub_id=@pub_id"; SqlParameter PubIdPar=new SqlParameter("@pub_id",SqlDbType.Char,4,"pub_id"); PubIdPar.SourceVersion=DataRowVersion.Original; deleteCmd.Parameters.Add(PubIdPar); SqlDataAdapter sqlAdapter=new SqlDataAdapter(); sqlAdapter.SelectCommand=selectCmd; sqlAdapter.DeleteCommand=deleteCmd; DataSet ds=new DataSet(); con.Open(); sqlAdapter.Fill(ds,"publishers"); DataTable table=new DataTable(); table=ds.Tables["publishers"]; table.PrimaryKey=new DataColumn[]//定义主键,便于查找 { table.Columns["pub_id"] }; DataRow row=table.Rows.Find(e.Item.Cells[2].Text); row.Delete(); sqlAdapter.Update(table); con.Close(); this.DataGrid1.EditItemIndex=-1; this.BindGrid(); } private void DataGrid1_Disposed(object sender, System.EventArgs e) { } 上一页 [1] [2] [3] [4] [5] [6] [7] [8] 下一页 |