關(guān)于GridView使用學(xué)習(xí)總結(jié)_第1頁(yè)
關(guān)于GridView使用學(xué)習(xí)總結(jié)_第2頁(yè)
關(guān)于GridView使用學(xué)習(xí)總結(jié)_第3頁(yè)
關(guān)于GridView使用學(xué)習(xí)總結(jié)_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、關(guān)于GridView使用學(xué)習(xí)總結(jié) 由于Asp.Net視頻比較舊,涉及到的數(shù)據(jù)綁定控件DataGrid在VS20xx中已經(jīng)沒(méi)有了,取而代之的是GridView。開(kāi)始覺(jué)得視頻中的例子沒(méi)法實(shí)現(xiàn)了,其實(shí)不然,DataGrid里面的功能GridView里一樣都不少,只是形式變化了一下,仔細(xì)研究一下發(fā)現(xiàn)它們是換湯不換藥啊。 (一)DataKeyName屬性 (1)DataKeyNames一般都是用來(lái)對(duì)當(dāng)前行做唯一標(biāo)示的,所以一般為數(shù)據(jù)庫(kù)的ID。 (2)GridView.DataKeyse.RowIndex,e.RowIndex是獲取事件對(duì)應(yīng)的行,GridView.DataKeyse.RowIndex就是獲

2、取對(duì)應(yīng)行的唯一標(biāo)示也就是DataKeyNames所指定列的值。 (3)DataList和Repeater是沒(méi)有的該屬性的。 在代碼中這樣使用:(定義的該函數(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 fr

3、om 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.DataKeyse.RowInde

4、x;/其中e為GridViewDelete(或者Edit)EventArgs e string empID = key0.ToString; (二)分頁(yè) 由于GridView中封裝了分頁(yè)的功能。這里實(shí)現(xiàn)起來(lái)很容易。先需要設(shè)置屬性:AllowPaging/PageSize/PageSetting。然后編寫(xiě)事件代碼: protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) this.GridView1.PageIndex = e.NewPageIndex; this.BindToDataG

5、ird; (三)排序 首先設(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"ord

6、er" = "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

7、 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; (四)刪除 這里需要注意一點(diǎn):就是獲取某一行的主鍵值。 protected voi

8、d GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) DataKey key = GridView1.DataKeyse.RowIndex; string empID = key0.ToString; SqlConnection con = DB.CreateCon; SqlCommand cmd = new SqlCommand(" from employees where employeeID= +empID+" , con); con.Open; cmd.ExecuteNonQuery; t

9、his.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.DataKeyse.RowIndex; string empID = key0.ToString; string lastName=(TextBox)(GridView1.Rows e.RowIndex .Cells 2.Controls 0

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論