当前位置:编程学习 > C#/ASP.NET >>

怎么实现GridView编辑、取消、更新和删除功能

  在VS2008下, GridView的数据源是通过调用业务对象来动态绑定的,我想通过点击GridView控件中“编辑”按钮,触发RowUpdting事件,实现修改当前行的数据,或者说是点击该按钮时,弹出一个无边框窗体来达到这个目的;同理,点击“删除”按钮时,触发RowDeleting事件,通过调用存储过程来实现删除的功能,在删除之前弹出对话框提醒用户“确定要删除吗?”,最后提示“删除成功”,反之“删除失败”,还请各路高手指点迷津! --------------------编程问答-------------------- 思路很清晰 楼主问什么  友情帮顶 --------------------编程问答-------------------- http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx --------------------编程问答--------------------

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:ButtonField CommandName="delete" Text="删除" />
            <asp:ButtonField CommandName="update" Text="修改" />
            <asp:ButtonField CommandName="add" Text="添加" />
        </Columns>
    </asp:GridView>



    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName=="delete")
        {
        //do delete
        }else if(e.CommandName=="update")
        {
        //do update
        }else if(e.CommandName=="add")
        {
        //do add
        }
    }
--------------------编程问答-------------------- 这篇文章已经讲的非常清楚了。
http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
--------------------编程问答--------------------
<asp:TemplateField HeaderText="操作" ItemStyle-HorizontalAlign="Center" ShowHeader="False">
<HeaderStyle Width="20%"></HeaderStyle>
 <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="True" CommandName="update">保存</asp:LinkButton>
                        <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="cancel">取消</asp:LinkButton>
                    </EditItemTemplate>
<ItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CausesValidation="false"  OnClientClick="return confirm('确认要编辑吗?');">编辑</asp:LinkButton>
                                      <asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" CausesValidation="False"  OnClientClick="return confirm('确认要删除吗?');">删除</asp:LinkButton>
                                      <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Select" CausesValidation="False" Visible="false" >选取</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
 protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
              BindData();
        }

        protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gv.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.gv.EditIndex = e.RowIndex;
            int id = int.Parse(this.gv.DataKeys[e.RowIndex].Value.ToString());
         
            this.gv.EditIndex = -1;
            BindData();
        }

        protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv.EditIndex = -1;
            BindData();
        }
--------------------编程问答--------------------   谢谢各位位的指导,这些功能现在已经成功的实现了! --------------------编程问答--------------------
 /// <summary>
    /// 绑定数据源
    /// </summary>
    public void bind()
    {
        SqlConnection con = new SqlConnection();
        DB.getCon(ref con);
        string sql = "select  * from picture";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds, "picture");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "id" };//主键
        GridView1.DataBind();
        //实现用“...”代替超长字符串
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            DataRowView dv;
            string gIntro;
            if (GridView1.PageIndex == 0)
            {
                dv = ds.Tables["picture"].DefaultView[i];
                gIntro = Convert.ToString(dv["image_size"]);
                GridView1.Rows[i].Cells[2].Text = SubStr(gIntro, 1);
            }
            else
            {
                dv = ds.Tables["picture"].DefaultView[i + (5 * GridView1.PageIndex)];
                gIntro = Convert.ToString(dv["image_size"]);
                GridView1.Rows[i].Cells[2].Text = SubStr(gIntro, 1);
            }

        }
        con.Close();
        GridView1.Columns[5].Visible = !GridView1.Columns[5].Visible;

    }
    public string SubStr(string s, int len)
    {
        if (s.Length <= len)
            return s;
        string snew = s.Substring(0, len);
        snew = snew + "...";
        return snew;
    }
    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from picture where id =" + GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlConnection con = new SqlConnection();
        DB.getCon(ref con);
        SqlCommand com = new SqlCommand(sqlstr, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        bind();
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = "update picture set ip='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()
            + "' where id=" + GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlConnection con = new SqlConnection();
        DB.getCon(ref con);
        SqlCommand com = new SqlCommand(sqlstr, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bind();
    }
    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    /// <summary>
    /// 选择
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cb1");
            if (true == CheckBox2.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox ) GridView1.Rows[i].FindControl("cb1");
            cb.Checked = false;
        }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,