打印本文 打印本文 关闭窗口 关闭窗口
(ASP.NET)修改和删除DataGrid行——数据库访问
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5238  更新时间:2009/4/23 10:47:11  文章录入:mintao  责任编辑:mintao
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]  下一页

打印本文 打印本文 关闭窗口 关闭窗口