ASP. NET data binding to the GridView Control
I have to say that the functions of the GridView control are indeed very powerful. A simple control can make data management very beautiful. Summarize some of the problems encountered in the GridView control during the tasks in the past two days;
①: The information in the database is displayed in the GridView control at will:
The GridView control has an AutoGenerateColumns attribute, which is used to control whether the GridView control automatically generates the associated columns during running. Generally, this attribute is set to false. Because we need a DIY GridView control. Click the arrow in the upper-right corner, select the edit column to add a BoundField field, select the data DataField attribute, and fill in the column name in the column that you want to display in the database, enter the name of the column to be displayed in the database in the HeaderText attribute. Click "OK" to display the following information:
Then add the link database code in the asp background. The author of the Code that links to the database has made a detailed introduction in the blog "[ASP] using the GRIDVIEW control to connect to the SQL SERVER database", which is not described in this article.
②: Implement the edit and delete function in the GridView control:
Click the arrow in the upper-right corner of the GridView control, select the edit column, add the CommandField field, and set the behavior attributes ShowDeleteButton and ShowEditButton to True. Click OK. The result is as follows:
However, editing and deletion does not have any function at this time. Because the GridView control has many events, it is only necessary to trigger the corresponding event to implement the edit and delete function.
First, we will introduce the first event-RowEditing. When you click Edit on the page, "change" and "cancel" will appear ". This event can be updated or canceled when you click Edit. RowCancelingEdit. When you click Edit on the running page, "change" and "cancel" appear, as shown in the running result:
Double-click this event and add the following code in the background:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.shuaxin(); }
The second event-RowCancelingEdit event RowCancelingEdit is to cancel the function. Double-click the event and enter the following code:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.shuaxin(); }
The third event, RowUpdating, implements the UPDATE function. Double-click the event to add the following code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { this.GridView1.EditIndex = e.RowIndex; string title = GridView1.DataKeys[e.RowIndex].Value.ToString(); string cotent = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; string strsql = "update activities set cotent='" + cotent + "' where title='" + title + "'"; SqlConnection con = new SqlConnection(ConfigurationManager. ConnectionStrings["username"].ConnectionString); SqlCommand cmd = new SqlCommand(strsql, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; this.shuaxin(); }
The fourth event, RowDeleting. Double-click the event to add the following code:
Protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e) {string title = GridView1.DataKeys [e. rowIndex]. value. toString (); string delete = "delete activities where title = '" + title + "'"; SqlConnection con = new SqlConnection (ConfigurationManager. connectionStrings ["username"]. connectionString); SqlCommand cmd = new SqlCommand (delete, con); con. open (); cmd. executeNonQuery (); con. close (); GridView1.EditIndex =-1; this. shuaxin (); // Method for self-writing database links ;}
Appendix: shuaxin (); Code:
private void shuaxin() { SqlConnection sqlcon = new SqlConnection(ConfigurationManager. ConnectionStrings["username"].ConnectionString); sqlcon.Open(); SqlDataAdapter da = new SqlDataAdapter(@"select * from activities", sqlcon); DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { GridView1.DataSource = ds; GridView1.DataBind(); } sqlcon.Close(); }
Note: The GridView control has a DataKeyNames attribute. Setting datakeyname is the primary key for retrieving data for this row when you click a row,
To ensure the accuracy of deleting updates. If this attribute is not set, the following result is displayed:
The above is about the use of ASP. NET data to bind the GridView control, I hope to help you learn.
Articles you may be interested in:
- Implementation of batch Delete gridview under asp.net
- Asp.net gridview 72 stunt
- In the asp.net GridView control, the template column CheckBox is fully selected, deselected, and canceled.
- Use of superlinks in asp.net GridView (with parameters)
- ASP. NET Gridview and checkbox all selected, all deselected implementation code
- Summary of methods for obtaining row indexes in the Rowcommand of asp.net gridview
- Asp.net simple code sets the implementation idea and code of the GridView adaptive column width without deformation
- Text Content in ASP. NET GridView cannot wrap (automatic line feed/normal line feed)
- Instance code for querying, paging, editing, updating, and deleting the gridview in asp.net
- Asp.net GridView usage (pagination)