【ASP.NET】GRIDVIEW控件的一般使用技巧


不得不說GridView控件的功能確實很強大,一個簡簡單單的控件就可以把數據管理的很美。在這兩天做的任務中碰到的一些GridView控件中遇到的問題進行總結;

:在GridView控件中隨意顯示數據庫中的信息:

GridView控件中有一個AutoGenerateColumns屬性,它的作用就是控制GridView控件是否在運行的時候自動生成相關聯的列,一般情況下把這個屬性設置成為false。因為我們需要的是一個DIYGridView控件。然后點擊右上角的箭頭,選擇編輯列添加一個BoundField字段,選擇數據DataField屬性,在后面填上自己想要顯示數據庫中某一列的列名,在外觀HeaderText屬性中填寫數據庫中要顯示的列名加以提示。然后點擊確定控件中就會顯示如下圖所示:

然后在asp后台中添加鏈接數據庫代碼就ok了。關於鏈接數據庫的代碼博主在博文ASP】用GRIDVIEW控件連接SQL SERVER數據庫中已經做了詳細介紹,本文就不多說了。

:在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是要在點擊行時獲得該行數據的主鍵,

以保證刪除更新時准確性;若沒有設置此屬性就會出現如下結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM