97骚碰,毛片大片免费看,亚洲第一天堂,99re思思,色好看在线视频播放,久久成人免费大片,国产又爽又色在线观看

GridView使用學(xué)習總結

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

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

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

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

 。ㄒ唬〥ataKeyName屬性

 。1)DataKeyNames一般都是用來(lái)對當前行做唯一標示的,所以一般為數據庫的ID。

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

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

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

  /// 實(shí)現數據綁定功能 ///

  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"); //將查詢(xún)到的數據添加到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í)現起來(lái)很容易。先需要設置屬性:AllowPaging/PageSize/PageSetting。然后編寫(xiě)事件代碼:

  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(); }

 。ㄋ模﹦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; //設置索引值為負取消編輯。 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中某列中控件強制轉換為T(mén)extBox,然后取出它的值。 Response.Write(empID +"&" + lastName ); //用于測試。 this.GridView1.EditIndex = -1; this.BindToDataGird(); }

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

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

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

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

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

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

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

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

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

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

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