利用GridView编辑和删除数据

本文详细介绍了如何使用GridView进行数据展示、编辑和删除操作。通过实例演示了GridView的初始化、数据绑定及事件处理过程,包括设置主键字段、处理删除和编辑事件的完整代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用GridView删除数据

第一步创建一个GridView

编辑列,填充字段属性

设置字段属性

其他字段根据数据库字段设置

接下来是添加“编辑”和“删除”了。

使用“添加新列”来添加列

添加“编辑”列

同理,添加“删除”

GridView设置效果如下:

初始化GridView

初始化数据从数据库查询的源码如下:

protected void bindSupplier()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select * from Supplier;";

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            DataSet ds = new DataSet();
            da.Fill(ds);

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();// 绑定并显示数据
        }

初始化:

if (!IsPostBack)
{
     // 绑定初始化供应商信息
     bindSupplier();
}

同时注意引入命名空间

运行程序浏览器效果图如下:

GridView的删除

其中“删除”链接需要处理的事件如下:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

接下来就是写代码了:

        // GridView的RowDataBound事件在数据被分别绑定到行时触发
        // e.Row返回“删除”连接按钮的所在行对象
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)// 判断数据行
            {
                try
                {
                    // 获取“删除”链接按钮
                    LinkButton linkButton_delete = (LinkButton)e.Row.Cells[4].Controls[0];
                    // 添加JavaScript代码实现客户端信息提示
                    linkButton_delete.OnClientClick = "return confirm('您真要删除分类名称为" + e.Row.Cells[1].Text + "的记录吗?');";
                }
                catch
                {
                    // 若try块有异常,不做任何处理
                }
            }
        }

        // 点击【删除】链接后触发的事件
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            //e.RowIndex:当前用户点击第几行的索引号
            //DataKey[0]:在主键列中查找第一个列
            cmd.CommandText = "delete from Supplier where SuppId="+ GridView1.DataKeys[e.RowIndex].Value.ToString()+";";

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            // 刷新GridView数据
            LinkButton_providerManage_Click(null, null);
        }

运行程序,报错:

错误原因是没有设置主键字段,解决:

再次运行就会删除成功了。

GridView的编辑

要处理的事件如下:

即:

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

事件处理完整代码如下:

        // 进入编辑状态
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            // 绑定初始化数据
            bindSupplier();
        }

        // 取消编辑状态
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            // 绑定初始化数据
            bindSupplier();
        }

        // 点击【更新】链接后触发的事件
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "update Supplier set Name='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text + "',Addr1='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text + "',Addr2='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text + "',City='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text + "',State='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text + "',Zip='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text + "',Phone='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text + "' where SuppId=" + GridView1.DataKeys[e.RowIndex].Value.ToString() + ";";

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            // 刷新GridView数据
            LinkButton_providerManage_Click(null, null);
        }

编辑更新成功

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值