ASP.NET資料繫結GridView控制項提示_實用技巧

來源:互聯網
上載者:User

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

①:在GridView控制項中隨意顯示資料庫中的資訊:

GridView控制項中有一個AutoGenerateColumns屬性,它的作用就是控制GridView控制項是否在啟動並執行時候自動產生相關聯的列,一般情況下把這個屬性設定成為false。因為我們需要的是一個DIY的GridView控制項。然後點擊右上方的箭頭,選擇編輯列添加一個BoundField欄位,選擇資料DataField屬性,在後面填上自己想要顯示資料庫中某一列的列名,在外觀HeaderText屬性中填寫資料庫中要顯示的列名加以提示。然後點擊確定控制項中就會顯示如下圖所示:

 

然後在asp後台中添加連結資料庫代碼就ok了。關於連結資料庫的代碼博主在博文“【ASP】用GRIDVIEW控制項串連SQL SERVER資料庫”中已經做了詳細介紹,本文就不多說了。

②:在GridView控制項中實現編輯刪除的功能:

點擊GridView控制項右上方的箭頭,選擇編輯列,添加CommandField欄位,設定此欄位行為屬性ShowDeleteButton和ShowEditButton為True。點擊確定即可。結果如圖下所示:

 

 但是此時的編輯刪除不會有任何功能。因為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是要在點擊行時獲得該行資料的主鍵,

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

以上就是關於ASP.NET資料繫結GridView控制項提示,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.