gridview

不得不说GridView控件的功能确实很强大,一个简简单单的控件就可以把数据管理的很美。在这两天做的任务中碰到的一些GridView控件中遇到的问题进行总结;

:在GridView控件中随意显示数据库中的信息:

GridView控件中有一个AutoGenerateColumns属性,它的作用就是控制GridView控件是否在运行的时候自动生成相关联的列,一般情况下把这个属性设置成为false。因为我们需要的是一个DIYGridView控件。然后点击右上角的箭头,选择编辑列添加一个BoundField字段,选择数据DataField属性,在后面填上自己想要显示数据库中某一列的列名,在外观HeaderText属性中填写数据库中要显示的列名加以提示。然后点击确定控件中就会显示如下图所示:

然后在asp后台中添加链接数据库代码就ok了。

:在GridView控件中实现编辑删除的功能:

点击GridView控件右上角的箭头,选择编辑列,添加CommandField字段,设置此字段行为属性ShowDeleteButtonShowEditButtonTrue。点击确定即可。结果如图下所示: 

 

 

但是此时的编辑删除不会有任何功能。因为GridView控件中有好多事件,实现编辑删除功能是要触发相应的事件才可以用。

首先介绍第一个事件——RowEditing。运行页面的时候点击编辑会出现更改取消。此事件的作用就是点击编辑时可以显示更新和取消。 RowCancelingEdit。运行页面的时候点击编辑会出现更改取消,运行结果如下图所示:

 

双击此事件,在后台添加代码如下:

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {

            GridView1.EditIndex = e.NewEditIndex;

            this.shuaxin();

        }

第二个事件——RowCancelingEdit       事件RowCancelingEdit就是实现取消功能。双击此事件填写代码如下:

     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

     {

        GridView1.EditIndex = -1;

        this.shuaxin();

     }

第三个事件——RowUpdating实现更新功能,双击此事件添加代码如下:

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

           this.GridView1.EditIndex = e.RowIndex;

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

           string cotent = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;

 

           string strsql = "update activities set cotent='" + cotent + "'

                             where title='" + title + "'";

           SqlConnection con = new SqlConnection(ConfigurationManager.

                                ConnectionStrings["username"].ConnectionString);

           SqlCommand cmd = new SqlCommand(strsql, con);

           con.Open();

           cmd.ExecuteNonQuery();

           con.Close();

           GridView1.EditIndex = -1;

           this.shuaxin();

        }

第四个事件——RowDeleting。此事件实现删除功能,双击事件添加代码如下:

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

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

 

            string delete = "delete activities  where title='" + title + "'";

 

            SqlConnection con = new SqlConnection(ConfigurationManager.

                                ConnectionStrings["username"].ConnectionString);

            SqlCommand cmd = new SqlCommand(delete, con);

            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();

            GridView1.EditIndex = -1;

            this.shuaxin();    //自己写的链接数据库的方法;

        }

附:shuaxin();代码:

        private void shuaxin()

        {

            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.

                                  ConnectionStrings["username"].ConnectionString);

            sqlcon.Open();

            SqlDataAdapter da = new SqlDataAdapter(@"select * from activities", sqlcon);

            DataSet ds = new DataSet();

            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)

            {

                GridView1.DataSource = ds;

                GridView1.DataBind();

            }

            sqlcon.Close();

        }

注:GridView控件中有一个DataKeyNames属性,设置datakeyname是要在点击行时获得该行数据的主键,

以保证删除更新时准确性;若没有设置此属性就会出现如下结果:

 

 

 

用gridview连接数据库

1 在新建的网站中添加一个Web窗体(默认窗体为default.aspx);

2 双击对象资源管理器中的Web.config对象,在该对象的设计代码窗口中添加如下代码,实现设置连接SQL Server数据库字符串的功能。 

<connectionStrings><!--连接数据库--> <add name="connection" connectionString="data source=.;integrated security=SSPI;initial catalog=newsfabu"></add> </connectionStrings>   

3 双击Default.aspx在设计的界面中添加一个GridView控件。

4 双击Default.aspx.cs在代码区添加如下代码。

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            GridViewBind();      //调用绑定数据信息函数

        }

    }

     public void GridViewBind()

    {

        this.Title = "新闻发布中心";

        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.

                               ConnectionStrings["connection"].ConnectionString);

        sqlcon.Open();

        SqlDataAdapter adsa = new SqlDataAdapter("select * from news", sqlcon);

        DataSet ds = new DataSet();

        adsa.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)

        {

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

        sqlcon.Close();

    }

附:ASP连接数据库,并实现增删改的功能:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            GridViewBind();      //调用绑定数据信息函数

        }

    }

 

    public void GridViewBind()

    {

        this.Title = "新闻发布中心";

        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.

                               ConnectionStrings["connection"].ConnectionString);

        sqlcon.Open();

        SqlDataAdapter adsa = new SqlDataAdapter("select * from news", sqlcon);

        DataSet ds = new DataSet();

        adsa.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)

        {

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

        sqlcon.Close();

    }

 

    protected void 添加新闻_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text == "" || TextBox2.Text == "")

        {

            this.Page.RegisterStartupScript("ss", "<script>alert('请输入编号')</script>");

        }

        else

        {

            SqlConnection con = new SqlConnection(ConfigurationManager.

                                ConnectionStrings["connection"].ConnectionString);

 

            string insert = "insert into news(新闻编号,新闻类别,新闻题目,新闻内容)

                            values(@_id,@_leibie,@_timu,@_neirong)";

 

            SqlCommand cmd=new SqlCommand (insert,con);

            cmd.Parameters.Add("@_id", SqlDbType.Int);

            cmd.Parameters["@_id"].Value = TextBox1.Text.Trim();

            cmd.Parameters.Add("@_leibie", SqlDbType.VarChar);

            cmd.Parameters["@_leibie"].Value = TextBox2.Text.Trim();

            cmd.Parameters.Add("@_timu", SqlDbType.VarChar);

            cmd.Parameters["@_timu"].Value = TextBox3.Text.Trim();

            cmd.Parameters.Add("@_neirong", SqlDbType.VarChar);

            cmd.Parameters["@_neirong"].Value = TextBox4.Text.Trim();

            con.Open();

            cmd.ExecuteNonQuery();

            GridViewBind();

 

        }

    }

 

    protected void 删除新闻_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text == "")

        {

            this.Page.RegisterStartupScript("ss", "<script>alert('请输入你想删除的编号')</script>");

        }

        else

        {

            SqlConnection con = new SqlConnection(ConfigurationManager.

                               ConnectionStrings["connection"].ConnectionString);

            string delete = "delete news from news where 新闻编号='" + TextBox1.Text + "'";

            SqlCommand com = new SqlCommand(delete, con);

            con.Open();

            com.ExecuteNonQuery();

            con.Close();

            GridViewBind();

        }

    }

    protected void 修改新闻_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text == "")

        {

            this.Page.RegisterStartupScript("ss", "<script>alert('请输入你要删除的编号')</script>");

        }

        else

        {

            SqlConnection con = new SqlConnection(ConfigurationManager.

                                ConnectionStrings["connection"].ConnectionString);

            string update = "update news set 新闻类别='" + TextBox2.Text + "',

                           新闻题目='" + TextBox3.Text + "',新闻内容='" +

                           TextBox4.Text + "' where 新闻编号='" + TextBox1.Text + "'";

            SqlCommand cmd = new SqlCommand(update, con);

            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();

            GridViewBind();

        }

    }

 

posted on 2015-02-17 15:34  飞哥Penfield  阅读(161)  评论(0编辑  收藏  举报

导航