Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand GridView

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand GridView". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand GridView.

(1) DataKeyName attribute

(1) DataKeyNames is generally used to uniquely mark the current row, so it is generally the ID of the database.

(2) GridView.DataKeys [e.RowIndex], e.RowIndex is the row corresponding to the get event, and GridView.DataKeys [e.RowIndex] is the only indication of the corresponding row, that is, the value of the column specified by DataKeyNames.

(3) DataList and Repeater do not have this attribute.

Use in the code like this: (the defined function needs to be called below)

/ 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"); / / add the queried data to DataSet. This.GridView1.DataKeyNames = new string [] {"employeeID"}; / / use of DataKeyNames this.GridView1.DataSource = ds.Tables ["emp"]; this.DataBind ();}

How to get the value?

DataKey key = GridView1.DataKeys [e.RowIndex]; / / where e is GridViewDelete (or Edit) EventArgs e string empID = key [0] .ToString ()

(2) pagination

Because the paging function is encapsulated in GridView. It's easy to implement here. First you need to set the property: AllowPaging/PageSize/PageSetting. Then write the event code:

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

(3) sorting

First, set the AllowSorting property to true. Event code:

Protected void GridView1_Sorting (object sender, GridViewSortEventArgs e) {if (ViewState ["order"] = = null) / / sets bidirectional sorting using ViewState. {ViewState ["order"] = "ASC";} else {if (ViewState ["order"]. ToString () = "ASC") {ViewState ["order"] = "DESC";} else {ViewState ["order"] = "ASC";} / / data binding shows 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 (); / / set sort this.GridView1.DataSource = ds.Tables [" emp "] .DefaultView; / / use the default view of the table as the data source. This.DataBind ();}

(IV) deletion

One thing to note here is to get the primary key value of a row.

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

(v) Editing (updating and canceling)

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; / / set the index value to negative to cancel editing. 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; / / casts a control in a column in GridView to TextBox, and then fetches its value. Response.Write (empID + "&" + lastName); / / for testing. This.GridView1.EditIndex =-1; this.BindToDataGird ();}

Attached result diagram:

At this point, I believe you have a deeper understanding of "how to understand GridView". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report