不得不說GridView控件的功能確實很強大,一個簡簡單單的控件就可以把數(shù)據(jù)管理的很美。在這兩天做的任務中碰到的一些GridView控件中遇到的問題進行總結;
①:在GridView控件中隨意顯示數(shù)據(jù)庫中的信息:
GridView控件中有一個AutoGenerateColumns屬性,它的作用就是控制GridView控件是否在運行的時候自動生成相關聯(lián)的列,一般情況下把這個屬性設置成為false。因為我們需要的是一個DIY的GridView控件。然后點擊右上角的箭頭,選擇編輯列添加一個BoundField字段,選擇數(shù)據(jù)DataField屬性,在后面填上自己想要顯示數(shù)據(jù)庫中某一列的列名,在外觀HeaderText屬性中填寫數(shù)據(jù)庫中要顯示的列名加以提示。然后點擊確定控件中就會顯示如下圖所示:
然后在asp后臺中添加鏈接數(shù)據(jù)庫代碼就ok了。關于鏈接數(shù)據(jù)庫的代碼博主在博文“【ASP】用GRIDVIEW控件連接SQL SERVER數(shù)據(jù)庫”中已經(jīng)做了詳細介紹,本文就不多說了。
②:在GridView控件中實現(xiàn)編輯刪除的功能:
點擊GridView控件右上角的箭頭,選擇編輯列,添加CommandField字段,設置此字段行為屬性ShowDeleteButton和ShowEditButton為True。點擊確定即可。結果如圖下所示:
但是此時的編輯刪除不會有任何功能。因為GridView控件中有好多事件,實現(xiàn)編輯刪除功能是要觸發(fā)相應的事件才可以用。
首先介紹第一個事件——RowEditing。運行頁面的時候點擊編輯會出現(xiàn)“更改”和“取消”。此事件的作用就是點擊編輯時可以顯示更新和取消。 RowCancelingEdit。運行頁面的時候點擊編輯會出現(xiàn)“更改”和“取消”,運行結果如下圖所示:
雙擊此事件,在后臺添加代碼如下:
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屬性,設置datakeyname是要在點擊行時獲得該行數(shù)據(jù)的主鍵,
以保證刪除更新時準確性;若沒有設置此屬性就會出現(xiàn)如下結果:
以上就是關于ASP.NET數(shù)據(jù)綁定GridView控件使用技巧,希望對大家的學習有所幫助。
您可能感興趣的文章:- 淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結
- 詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法
- 總結Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
- ASP.NET數(shù)據(jù)綁定之Repeater控件
- ASP.NET數(shù)據(jù)綁定之GridView控件
- ASP.NET數(shù)據(jù)綁定之DataList控件實戰(zhàn)篇
- ASP.NET數(shù)據(jù)綁定之DataList控件
- AspNetAjaxPager,Asp.Net通用無刷新Ajax分頁控件,支持多樣式多數(shù)據(jù)綁定
- ASP.NET數(shù)據(jù)綁定控件詳解