GridView使用學習總結_實用技巧

來源:互聯網
上載者:User

由於Asp.Net視頻比較舊,涉及到的資料繫結控制項DataGrid在VS2012中已經沒有了,取而代之的是GridView。開始覺得視頻中的例子沒法實現了,其實不然,DataGrid裡面的功能GridView裡一樣都不少,只是形式變化了一下,仔細研究一下發現它們是換湯不換藥啊。
(一)DataKeyName屬性
(1)DataKeyNames一般都是用來對當前行做唯一標示的,所以一般為資料庫的ID。
(2)GridView.DataKeys[e.RowIndex],e.RowIndex是擷取事件對應的行,GridView.DataKeys[e.RowIndex]就是擷取對應行的唯一標示也就是DataKeyNames所指定列的值。

(3)DataList和Repeater是沒有的該屬性的。

在代碼中這樣使用:(定義的該函數在下面都需要調用)

/// <summary> /// 實現資料繫結功能 /// </summary> private void BindToDataGird()   {  SqlConnection con = DB.CreateCon();  SqlDataAdapter sda = new SqlDataAdapter();  sda.SelectCommand = new SqlCommand("select employeeID,FirstName,LastName,Title,BirthDate from employees ", con);  DataSet ds = new DataSet();  sda.Fill(ds, "emp");   //將查詢到的資料添加到DataSet中。  this.GridView1.DataKeyNames =new string[]{ "employeeID"}; //DataKeyNames的使用  this.GridView1.DataSource = ds.Tables["emp"];   this.DataBind(); } 

如何取值?

DataKey key = GridView1.DataKeys[e.RowIndex];//其中e為GridViewDelete(或者Edit)EventArgs e string empID = key[0].ToString(); 


(二)分頁
由於GridView中封裝了分頁的功能。這裡實現起來很容易。先需要設定屬性:AllowPaging/PageSize/PageSetting。然後編寫事件代碼:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {  this.GridView1.PageIndex = e.NewPageIndex;  this.BindToDataGird(); } 


(三)排序
首先設定AllowSorting屬性為true.事件代碼:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {  if (ViewState["order"] == null)  //使用ViewState設定雙向排序。  {   ViewState["order"] = "ASC";  }  else  {   if (ViewState["order"].ToString() == "ASC")   {    ViewState["order"] = "DESC";   }   else   {    ViewState["order"] = "ASC";   }  }  //資料繫結顯示  SqlConnection con = DB.CreateCon();  SqlDataAdapter sda = new SqlDataAdapter();  sda.SelectCommand = new SqlCommand("select employeeID,FirstName,LastName,Title,BirthDate from employees ", con);  DataSet ds = new DataSet();  sda.Fill(ds, "emp");  ds.Tables["emp"].DefaultView.Sort = e.SortExpression + " " + ViewState["order"].ToString(); //設定排序  this.GridView1.DataSource = ds.Tables["emp"].DefaultView; //將表的預設視圖作為資料來源。  this.DataBind(); } 


(四)刪除
這裡需要注意一點:就是擷取某一行的主索引值。

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {  DataKey key = GridView1.DataKeys[e.RowIndex];  string empID = key[0].ToString();  SqlConnection con = DB.CreateCon();  SqlCommand cmd = new SqlCommand("delete from employees where employeeID= '"+empID+"'" , con);  con.Open();  cmd.ExecuteNonQuery();  this.BindToDataGird(); } 

(五)編輯(更新和取消)

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {  this.GridView1.EditIndex = e.NewEditIndex;  this.BindToDataGird(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  this.GridView1.EditIndex = -1; //設定索引值為負取消編輯。  this.BindToDataGird(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {  DataKey key = GridView1.DataKeys[e.RowIndex];  string empID = key[0].ToString();  string lastName=((TextBox)(GridView1.Rows [e.RowIndex ] .Cells [2].Controls [0])).Text ; //將GridView中某列中控制項強制轉換為TextBox,然後取出它的值。  Response.Write(empID +"&" + lastName ); //用於測試。  this.GridView1.EditIndex = -1;  this.BindToDataGird(); } 

附結果圖:

小結:資料繫結控制項:Reapter/DataList/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.