gridview中的修改,删除等问题

1.前台代码: 

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AutoGenerateColumns="False" DataKeyNames="Cno"
                        BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
                        CellPadding="4" CellSpacing="2" ForeColor="Black" OnPageIndexChanging="GridView1_PageIndexChanging1"
                        OnRowDeleting="GridView1_RowDeleting">
                        <Columns>
                            <asp:BoundField DataField="Cno" HeaderText="编号" ReadOnly="True" ItemStyle-Width="180px"
                                SortExpression="Cno" />
                            <asp:BoundField DataField="Cname" HeaderText="姓名" SortExpression="Cname" ItemStyle-Width="180px" />
                            <asp:BoundField DataField="Clocation" HeaderText="地址" ItemStyle-Width="180px" SortExpression="Clocation" />
                            <asp:BoundField DataField="Cphone" HeaderText="电话" ItemStyle-Width="180px" SortExpression="Cphone" />
                            <asp:HyperLinkField DataNavigateUrlFields="Cno" DataNavigateUrlFormatString="UpdateClient2.aspx?Cno={0}"
                                HeaderText="修改" Text="修改" />
                            <%--DataNavigateUrlFormatString:点击GridView中的HeaderText为“编号”的列的时候会转向queryeach.aspx页面--%>
                            <asp:TemplateField HeaderText="删除">
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                                        Text="删除" OnClientClick="return confirm('确定要删除吗?')"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="#ADFF2F" Font-Bold="True" ForeColor="Black" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>

2.后台代码:
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class   UpdateClient: System.Web.UI.Page   //updateclient为aspx文件名
{

    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=.;Database=数据库名;Uid=sa;Pwd=123";
    protected void Page_Load(object sender, EventArgs e)       //page_load函数
    {

        
        if (!IsPostBack)
        {
             ClientBind();
        }
    }

 

    private void ClientBind()  //绑定函数
    {
        try
        {
            string sqlstr = "select * from Client order by Cno ASC";       //database为Client

             //string sqlstr = "select * from 表";

             sqlcon = new SqlConnection(strCon);   //从文件Web.config中读取连接字符串 
             SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);    //创建SqlDataAdapter对象,调用存储过程

             sqlcon.Open();
             DataSet myds = new DataSet();      //创建并填充DataSet
             myda.Fill(myds, "表");
             GridView1.DataSource = myds;
             GridView1.DataKeyNames = new string[] { "id" };//主键
             GridView1.DataBind();
             sqlcon.Close(); 
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

//删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        string str = "delete from Client where Cno='" + this.GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        con.Open();
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        con.Close();
        ClientBind();
    }

//更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = "update 表 set 字段1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        SqlCommand sqlcom=new SqlCommand(sqlstr,con);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }

//取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
}

转载于:https://www.cnblogs.com/yijincheng/p/5102372.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值