GridView选中,编辑,取消,删除

来源:互联网 发布:剪力弯矩图软件 编辑:程序博客网 时间:2024/05/21 23:52

效果图:

后台代码:
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 _Default : System.Web.UI.Page
{
    SqlConnection conn=new SqlConnection();
    SqlCommand cmd=new SqlCommand();
    string strCon = "Data Source=.\\SQLExpress;Initial Catalog=Message;User ID=sa;Password=123";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)        //if(不是回发),点击一个按钮,页面不会重新生成
        {
            bind();
        }
    }
   
    public void bind()
    {
        string sqlStr = "select * from Employee";
        conn = new SqlConnection(strCon);                         //连接字符串
        SqlDataAdapter myda = new SqlDataAdapter(sqlStr, conn);   //创建DataAdapter数据适配器实例
        DataSet myds = new DataSet();                               //创建DataSet实例
        conn.Open();                                            //打开数据库
        myda.Fill(myds, "Employee");                             //使用DataAdapter的Fill方法填充
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };             
        GridView1.DataBind();                                    //绑定数据
        conn.Close();                                          //关闭数据库
    }
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from Employee where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        conn = new SqlConnection(strCon);
        cmd = new SqlCommand(sqlstr, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }
    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;    //设置gridview的编辑项。gridview.编辑项下标=e.新的编辑项下标。
        bind();
    }
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        conn = new SqlConnection(strCon);
        string sqlstr = "update Employee set 身份证号码='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',姓名='" 
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',员工性别='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',家庭住址='" 
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where ID='" 
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        cmd = new SqlCommand(sqlstr, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        GridView1.EditIndex = -1;
        bind();
    }
}


前台主要代码:
                            ... ...
<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="身份证号码" HeaderText="用户ID" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                            <asp:BoundField DataField="员工性别" HeaderText="性别" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

0 0
原创粉丝点击