国产午夜精品久久久久免费视-国产午夜三级-国产性大片黄在线观看在线放-国产性老妇女做爰在线-一区一精品-一区在线观看

GridView使用學(xué)習(xí)總結(jié)

時(shí)間:2020-11-24 16:12:18 學(xué)習(xí)總結(jié) 我要投稿

關(guān)于GridView使用學(xué)習(xí)總結(jié)

  由于Asp.Net視頻比較舊,涉及到的數(shù)據(jù)綁定控件DataGrid在VS2012中已經(jīng)沒有了,取而代之的是GridView。開始覺得視頻中的例子沒法實(shí)現(xiàn)了,其實(shí)不然,DataGrid里面的功能GridView里一樣都不少,只是形式變化了一下,仔細(xì)研究一下發(fā)現(xiàn)它們是換湯不換藥啊。

關(guān)于GridView使用學(xué)習(xí)總結(jié)

  (一)DataKeyName屬性

  (1)DataKeyNames一般都是用來對當(dāng)前行做唯一標(biāo)示的,所以一般為數(shù)據(jù)庫的ID。

 。2)GridView.DataKeys[e.RowIndex],e.RowIndex是獲取事件對應(yīng)的行,GridView.DataKeys[e.RowIndex]就是獲取對應(yīng)行的唯一標(biāo)示也就是DataKeyNames所指定列的值。

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

  在代碼中這樣使用:(定義的`該函數(shù)在下面都需要調(diào)用)

  /// 實(shí)現(xiàn)數(shù)據(jù)綁定功能 ///

  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"); //將查詢到的數(shù)據(jù)添加到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中封裝了分頁的功能。這里實(shí)現(xiàn)起來很容易。先需要設(shè)置屬性:AllowPaging/PageSize/PageSetting。然后編寫事件代碼:

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

 。ㄈ┡判

  首先設(shè)置AllowSorting屬性為true.事件代碼:

  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (ViewState["order"] == null) //使用ViewState設(shè)置雙向排序。 { ViewState["order"] = "ASC"; } else { if (ViewState["order"].ToString() == "ASC") { ViewState["order"] = "DESC"; } else { ViewState["order"] = "ASC"; } } //數(shù)據(jù)綁定顯示 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(); //設(shè)置排序 this.GridView1.DataSource = ds.Tables["emp"].DefaultView; //將表的默認(rèn)視圖作為數(shù)據(jù)源。 this.DataBind(); }

 。ㄋ模﹦h除

  這里需要注意一點(diǎn):就是獲取某一行的主鍵值。

  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(" 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; //設(shè)置索引值為負(fù)取消編輯。 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中某列中控件強(qiáng)制轉(zhuǎn)換為TextBox,然后取出它的值。 Response.Write(empID +"&" + lastName ); //用于測試。 this.GridView1.EditIndex = -1; this.BindToDataGird(); }

【關(guān)于GridView使用學(xué)習(xí)總結(jié)】相關(guān)文章:

關(guān)于工作學(xué)習(xí)總結(jié)01-04

關(guān)于跟崗學(xué)習(xí)總結(jié)01-18

關(guān)于初中學(xué)習(xí)總結(jié)06-07

關(guān)于DCS操作學(xué)習(xí)總結(jié)01-07

關(guān)于個(gè)人學(xué)習(xí)總結(jié)01-04

關(guān)于使用林地的請示10-12

關(guān)于教師學(xué)習(xí)總結(jié)4篇02-12

關(guān)于教師學(xué)習(xí)總結(jié)8篇02-12

關(guān)于學(xué)習(xí)總結(jié)的作文范文01-30

關(guān)于教師學(xué)習(xí)總結(jié)三篇01-28

主站蜘蛛池模板: 国产一级毛片国产 | 国产精品午夜波多野结衣性色 | 欧美一区二区三区免费不卡 | 黄色视屏在线 | 日韩欧美 在线播放 | 黄色成人在线 | 黄免费看| 久久久久久福利 | 日韩不卡高清视频 | 国产成人激情视频 | 大学生美女穿黑色丝袜网站 | 国产精品麻豆免费版 | 色偷偷人人澡人人爽人人模 | 成人看视频 | 欧美黄色免费在线观看 | h网站免费观看 | 日本一区二区免费在线观看 | 国产亚洲欧美另类一区二区三区 | 黄色三级在线观看 | 精品一区二区三区在线观看 | 2022久久国产精品免费热麻豆 | 国产一精品一av一免费爽爽 | 看黄色一级视频 | 日本高清中文字幕一区二区三区a | 福利在线观看视频 | 欧美成免费 | 欧美成人精品手机在线观看 | 国产免费叼嘿在线观看 | 免费大香伊蕉在人线国产 | 日本三级香港三级人妇网站 | 欧美色图日韩色图 | 美女视频网站黄色 | 高清国产美女一级a毛片在线 | 成年人网站在线观看免费 | 国产欧美日韩在线 | 狠狠色狠色综合曰曰 | 黄网站免费看 | www.国产成人 | 久久亚洲热| 激情五月激情综合色区 | 大杳焦伊人久久综合热 |