ASP 中 GridView 的粗浅入门

    针对GridView 的数据库绑定,以及编辑、删除的操作:

    首先建立如图所示的数据库表(SQL):数据库名为:student 表名为:student_info,表设计如图所示:

   

然后,打开vs,建立一个项目文件。拉出一个GridView控件如图:

单击图右上角的选择——编辑列,添加四个BoundField字段,在每个字段的headertext的属性上写:id、学生姓名、性别,年龄。同时分别把它们转换为templateField,方法:单击属性框下方的:

然后在字段CommandField上单击添加“编辑。更新、取消”和“删除”两个字段,确定结束。

回到GridView控件界面,打开编辑模版。出现下图:

添加lable控件:

点击[Lable1]出现DataBindings,如图所示:

在Text属性自定义表达式写上 Bind("id"),确定结束。回到GridView任务处选择如图所示的任务:

继续绑定 ,在Text属性自定义表达式写上 Bind("s_name")

重复以上操作,依次绑定Bind("s_sex")、Bind("s_age").全部绑定后回到主界面,就会变成:

(颜色不对只需更改套用的格式即可,没太大区别)

之后进入后台代码,写该函数:

public void bindtogrid()

        {

            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = "server=.;database=students;integrated security=true";

            SqlDataAdapter da = new SqlDataAdapter("select * from student_info", conn);

            DataSet ds = new DataSet();

            da.Fill(ds, "result");

            this.GridView1.DataSource = ds.Tables["result"];

            //this.GridView1.DataMember = "id";

            this.GridView1.DataBind();

        }

在 Page_Load事件中,调用该该函数即可

if (!IsPostBack)

            {

                bindtogrid();

            }

这样数据库就绑定车就绑定成功!在浏览器中即可查看!接下来要实现编辑更改、删除功能,回到aspx界面,在属性中找到DataKeysName写上“id”,然后在事件中分别触发以下操作:RowCancelingEdit、RowDeleting、RowEditing、Rowupdating事件,每个事件相对的后台代码如下图所示:

        //取消

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

        {

            this.GridView1.EditIndex = -1;

            bindtogrid();

        }

//删除

       protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

            string sid=this.GridView1.DataKeys[e.RowIndex].Values.ToString();//获取DataKeyName的值,数据库中的主键值

            string strsql="delete *  from student_info where id="+sid+"";

            SqlConnection conn=new SqlConnection();

            conn.ConnectionString="server=.;database=students;integrated security=true";

            SqlCommand cmd=new SqlCommand(strsql,conn);

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            bindtogrid();

        }

      //编辑

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {

            this.GridView1.EditIndex = e.NewEditIndex;

            bindtogrid();

        }

//更新

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            TextBox t1 = new TextBox();

            t1 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox1");

            string sid = t1.Text;

            TextBox t2 = new TextBox();

            t2 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox2");

            string sname = t2.Text;

            TextBox t3 = new TextBox();

            t3 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox3");

            string ssex = t3.Text;

            TextBox t4 = new TextBox();

            t4 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox4");

            string sage = t4.Text;

            string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();

            string strsql = "update student_info set id=" + sid + ",s_name='" + sname + "',s_sex='" + ssex + "',s_age=" + sage + " where id=" + id + "";

            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = "server=.;database=students;integrated security=true";

            SqlCommand cmd = new SqlCommand(strsql, conn);

            conn.Open();

            cmd.ExecuteNonQuery();

            this.GridView1.EditIndex = -1;

            conn.Close();

            bindtogrid();

        }

之后在浏览器中就能实现功能啦!

这样GridView简单的编辑、修改、删除功能就实现了。

原文地址:https://www.cnblogs.com/wynet/p/2467818.html