GridView 的增删改查

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
            OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="用户ID" ReadOnly="True" />
                <asp:BoundField DataField="AuthorName" HeaderText="用户名称" />
                <asp:BoundField DataField="Email" HeaderText="电子邮箱" />
                <asp:BoundField DataField="Age" HeaderText="年龄" />
                <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
            </Columns>
          
        </asp:GridView>

 

.cs

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }

        }

        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() + "'";
            //sqlcon = new SqlConnection(strCon);
            //sqlcom = new SqlCommand(sqlstr, sqlcon);
            //sqlcon.Open();
            //sqlcom.ExecuteNonQuery();
            //sqlcon.Close();
            bind();
        }

        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //sqlcon = new SqlConnection(strCon);
            //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() + "'";
            //sqlcom = new SqlCommand(sqlstr, sqlcon);
            //sqlcon.Open();
            //sqlcom.ExecuteNonQuery();
            //sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }

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

        //绑定
        public void bind()
        {
            string sqlstr = "select * from UserInfo";
            //sqlcon = new SqlConnection(strCon);
            //SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            //DataSet myds = new DataSet();
            //sqlcon.Open();
            //myda.Fill(myds, "表");

            //DataSet myds = ZH.DBUtility.SQLHelper.Query(sqlstr);
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "id" };//主键
            GridView1.DataBind();
            //sqlcon.Close();
        }

阅读更多
个人分类: asp.net
想对作者说点什么? 我来说一句

GridView增删改查学习示例源码

2013年01月11日 246KB 下载

ASP.NET GridView增删改查操作

2012年04月15日 304KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭