不得不說GridView控件的功能確實(shí)很強(qiáng)大,一個(gè)簡簡單單的控件就可以把數(shù)據(jù)管理的很美。在這兩天做的任務(wù)中碰到的一些GridView控件中遇到的問題進(jìn)行總結(jié);
①:在GridView控件中隨意顯示數(shù)據(jù)庫中的信息:
GridView控件中有一個(gè)AutoGenerateColumns屬性,它的作用就是控制GridView控件是否在運(yùn)行的時(shí)候自動(dòng)生成相關(guān)聯(lián)的列,一般情況下把這個(gè)屬性設(shè)置成為false。因?yàn)槲覀冃枰氖且粋€(gè)DIY的GridView控件。然后點(diǎn)擊右上角的箭頭,選擇編輯列添加一個(gè)BoundField字段,選擇數(shù)據(jù)DataField屬性,在后面填上自己想要顯示數(shù)據(jù)庫中某一列的列名,在外觀HeaderText屬性中填寫數(shù)據(jù)庫中要顯示的列名加以提示。然后點(diǎn)擊確定控件中就會(huì)顯示如下圖所示:

然后在asp后臺(tái)中添加鏈接數(shù)據(jù)庫代碼就ok了。關(guān)于鏈接數(shù)據(jù)庫的代碼博主在博文“【ASP】用GRIDVIEW控件連接SQL SERVER數(shù)據(jù)庫”中已經(jīng)做了詳細(xì)介紹,本文就不多說了。
②:在GridView控件中實(shí)現(xiàn)編輯刪除的功能:
點(diǎn)擊GridView控件右上角的箭頭,選擇編輯列,添加CommandField字段,設(shè)置此字段行為屬性ShowDeleteButton和ShowEditButton為True。點(diǎn)擊確定即可。結(jié)果如圖下所示:

但是此時(shí)的編輯刪除不會(huì)有任何功能。因?yàn)镚ridView控件中有好多事件,實(shí)現(xiàn)編輯刪除功能是要觸發(fā)相應(yīng)的事件才可以用。
首先介紹第一個(gè)事件——RowEditing。運(yùn)行頁面的時(shí)候點(diǎn)擊編輯會(huì)出現(xiàn)“更改”和“取消”。此事件的作用就是點(diǎn)擊編輯時(shí)可以顯示更新和取消。 RowCancelingEdit。運(yùn)行頁面的時(shí)候點(diǎn)擊編輯會(huì)出現(xiàn)“更改”和“取消”,運(yùn)行結(jié)果如下圖所示:

雙擊此事件,在后臺(tái)添加代碼如下:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.shuaxin();
}
第二個(gè)事件——RowCancelingEdit 事件RowCancelingEdit就是實(shí)現(xiàn)取消功能。雙擊此事件填寫代碼如下:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.shuaxin();
}
第三個(gè)事件——RowUpdating實(shí)現(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();
}
第四個(gè)事件——RowDeleting。此事件實(shí)現(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控件中有一個(gè)DataKeyNames屬性,設(shè)置datakeyname是要在點(diǎn)擊行時(shí)獲得該行數(shù)據(jù)的主鍵,
以保證刪除更新時(shí)準(zhǔn)確性;若沒有設(shè)置此屬性就會(huì)出現(xiàn)如下結(jié)果:

以上就是關(guān)于ASP.NET數(shù)據(jù)綁定GridView控件使用技巧,希望對大家的學(xué)習(xí)有所幫助。
您可能感興趣的文章:- 淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結(jié)
- 詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法
- 總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
- ASP.NET數(shù)據(jù)綁定之Repeater控件
- ASP.NET數(shù)據(jù)綁定之GridView控件
- ASP.NET數(shù)據(jù)綁定之DataList控件實(shí)戰(zhàn)篇
- ASP.NET數(shù)據(jù)綁定之DataList控件
- AspNetAjaxPager,Asp.Net通用無刷新Ajax分頁控件,支持多樣式多數(shù)據(jù)綁定
- ASP.NET數(shù)據(jù)綁定控件詳解