不得不說GridView控件的功能確實很強大,一個簡簡單單的控件就可以把數(shù)據(jù)管理的很美。在這兩天做的任務(wù)中碰到的一些GridView控件中遇到的問題進行總結(jié);
①:在GridView控件中隨意顯示數(shù)據(jù)庫中的信息:
GridView控件中有一個AutoGenerateColumns屬性,它的作用就是控制GridView控件是否在運行的時候自動生成相關(guān)聯(lián)的列,一般情況下把這個屬性設(shè)置成為false。因為我們需要的是一個DIY的GridView控件。然后點擊右上角的箭頭,選擇編輯列添加一個BoundField字段,選擇數(shù)據(jù)DataField屬性,在后面填上自己想要顯示數(shù)據(jù)庫中某一列的列名,在外觀HeaderText屬性中填寫數(shù)據(jù)庫中要顯示的列名加以提示。然后點擊確定控件中就會顯示如下圖所示:
然后在asp后臺中添加鏈接數(shù)據(jù)庫代碼就ok了。關(guān)于鏈接數(shù)據(jù)庫的代碼博主在博文“【ASP】用GRIDVIEW控件連接SQL SERVER數(shù)據(jù)庫”中已經(jīng)做了詳細介紹,本文就不多說了。
②:在GridView控件中實現(xiàn)編輯刪除的功能:
點擊GridView控件右上角的箭頭,選擇編輯列,添加CommandField字段,設(shè)置此字段行為屬性ShowDeleteButton和ShowEditButton為True。點擊確定即可。結(jié)果如圖下所示:
但是此時的編輯刪除不會有任何功能。因為GridView控件中有好多事件,實現(xiàn)編輯刪除功能是要觸發(fā)相應(yīng)的事件才可以用。
首先介紹第一個事件——RowEditing。運行頁面的時候點擊編輯會出現(xiàn)“更改”和“取消”。此事件的作用就是點擊編輯時可以顯示更新和取消。 RowCancelingEdit。運行頁面的時候點擊編輯會出現(xiàn)“更改”和“取消”,運行結(jié)果如下圖所示:
雙擊此事件,在后臺添加代碼如下:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.shuaxin(); }
第二個事件——RowCancelingEdit 事件RowCancelingEdit就是實現(xiàn)取消功能。雙擊此事件填寫代碼如下:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.shuaxin(); }
第三個事件——RowUpdating實現(xiàn)更新功能,雙擊此事件添加代碼如下:
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。此事件實現(xiàn)刪除功能,雙擊事件添加代碼如下:
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(); //自己寫的鏈接數(shù)據(jù)庫的方法; }
附: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屬性,設(shè)置datakeyname是要在點擊行時獲得該行數(shù)據(jù)的主鍵,
以保證刪除更新時準(zhǔn)確性;若沒有設(shè)置此屬性就會出現(xiàn)如下結(jié)果:
以上就是關(guān)于ASP.NET數(shù)據(jù)綁定GridView控件使用技巧,希望對大家的學(xué)習(xí)有所幫助。
標(biāo)簽:麗江 汕頭 婁底 重慶 宜春 本溪 河南 吉林
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET數(shù)據(jù)綁定GridView控件使用技巧》,本文關(guān)鍵詞 ASP.NET,數(shù)據(jù),綁定,GridView,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。