欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年09月18日 10:49:03   作者:wangyajin333  
這篇文章主要是關(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)它們是換湯不換藥啊。
(一)DataKeyName屬性
(1)DataKeyNames一般都是用來對(duì)當(dāng)前行做唯一標(biāo)示的,所以一般為數(shù)據(jù)庫(kù)的ID。
(2)GridView.DataKeys[e.RowIndex],e.RowIndex是獲取事件對(duì)應(yīng)的行,GridView.DataKeys[e.RowIndex]就是獲取對(duì)應(yīng)行的唯一標(biāo)示也就是DataKeyNames所指定列的值。

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

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

/// <summary> 
/// 實(shí)現(xiàn)數(shù)據(jù)綁定功能 
/// </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");   //將查詢到的數(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(); 


(二)分頁(yè)
由于GridView中封裝了分頁(yè)的功能。這里實(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(); 
} 


(四)刪除
這里需要注意一點(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("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; //設(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 ); //用于測(cè)試。 
 this.GridView1.EditIndex = -1; 
 this.BindToDataGird(); 
} 

附結(jié)果圖:

小結(jié):數(shù)據(jù)綁定控件:Reapter/DataList/GridView的功能成遞增關(guān)系,都使用到了模板。所以掌握模板很重要。視頻使用模板大都是使用控件,不是代碼。總感覺這里需要學(xué)習(xí)的地方還有很多。需要做例子鞏固使用。

相關(guān)文章

  • .NET 純分頁(yè)代碼實(shí)例

    .NET 純分頁(yè)代碼實(shí)例

    這篇文章介紹了.NET 純分頁(yè)代碼實(shí)例,有需要的朋友可以參考一下
    2013-09-09
  • ASP.NET Core發(fā)送郵件的方法

    ASP.NET Core發(fā)送郵件的方法

    這篇文章主要為大家詳細(xì)介紹了ASP.NET Core發(fā)送郵件的方法,告訴大家如何在ASP.NET Core中發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • asp.net中將數(shù)據(jù)庫(kù)綁定到DataList控件的實(shí)現(xiàn)方法與實(shí)例代碼

    asp.net中將數(shù)據(jù)庫(kù)綁定到DataList控件的實(shí)現(xiàn)方法與實(shí)例代碼

    我現(xiàn)在要做這個(gè)頁(yè)面,然后我用DataList控件顯示題目,我想問如何把我數(shù)據(jù)庫(kù)的題目綁定到DataList控件里面,然后讓它顯示考試題目?
    2011-06-06
  • ASP.NET?Core的日志系統(tǒng)介紹

    ASP.NET?Core的日志系統(tǒng)介紹

    這篇文章介紹了ASP.NET?Core的日志系統(tǒng),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • asp.net后臺(tái)如何動(dòng)態(tài)添加JS文件和css文件的引用

    asp.net后臺(tái)如何動(dòng)態(tài)添加JS文件和css文件的引用

    動(dòng)態(tài)添加JS文件和css文件的引用在asp.net后臺(tái)如何實(shí)現(xiàn)呢?首先添加命名空間 using System.Web.UI.HtmlControls,之后按照下面的步驟操作即可
    2014-09-09
  • AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程

    AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程

    MassTransit?Courier是一種用于創(chuàng)建和執(zhí)行帶有故障補(bǔ)償?shù)姆植际绞聞?wù)的機(jī)制,它可以用于滿足本地事務(wù)的需求,也可以在分布式系統(tǒng)中實(shí)現(xiàn)分布式事務(wù),這篇文章主要介紹了AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù),需要的朋友可以參考下
    2022-10-10
  • .NET Core日志配置的方法

    .NET Core日志配置的方法

    熟悉ASP.NET的開發(fā)者一定對(duì)web.config文件不陌生,這篇文章主要介紹了.NET Core日志配置的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • 小心!ASP.NET網(wǎng)站發(fā)布時(shí)的那些坑

    小心!ASP.NET網(wǎng)站發(fā)布時(shí)的那些坑

    ASP.NET網(wǎng)站發(fā)布時(shí)的那些坑,要小心了,為什么網(wǎng)站發(fā)布后,每個(gè)頁(yè)面第一次打開都很卡?ASP.NET session 頻繁丟失如何解決?具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • .NET6中使用CuteEditor詳解

    .NET6中使用CuteEditor詳解

    本文詳細(xì)講解了.NET6中使用CuteEditor的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • NetCore WebSocket即時(shí)通訊示例

    NetCore WebSocket即時(shí)通訊示例

    這篇文章主要為大家詳細(xì)介紹了NetCore WebSocket即時(shí)通訊示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論