Bind a data source using the GridView Control
The GridView control is a data table control that displays data in the data source as a table. Each column represents a field, and each line represents a record.
You can bind a GridView control to a data source by configuring data source binding and by binding code.
1. Bind The GridView control by configuring the data source
Next, configure the SqlDataScource control,
Next, we need to configure the GridView control. In the GridView control, select our data source SqlDataSource control, as shown in figure
After the configuration is complete, run the following command to check the effect, as shown in figure
2. Bind The GridView control by code
The above code can also be completed using the following code. Statement for loading events:
Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace DataShowApplication {public partial class Index: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// create a string to connect SqlConnection sqlconn = new SqlConnection ("Data Source =. \ CENA; Initial Catalog = Asp.net; Persist Security Info = True; User ID = sa; Password = 123456 "); SqlDataAdapter sqlda = new SqlDataAdapter (" select * from DemoStu ", sqlconn); DataSet ds = new DataSet (); sqlda. fill (ds); this. data_GridView.DataSource = ds; this. data_GridView.DataBind ();}}}
Custom columns of the GridView Control
Each column in The GridView control has a DataControlField object.
In the experiment, how to add the BoundField column to the GridView control to bind data to an instance is as follows:
Add an Index. aspx page and a GridView control and a SqlDataSoure control. For example:
To customize columns for the GridView control by row, you must cancel the function of Automatically Generating fields in the GridView. Set the AutoGeneraterColums attribute of the GridView to false.
Set the SqlDataSource control to configure the data source, and assign the SqlDataSource control to the performanceid attribute of the GridView control. For example:
Then edit the GridView control, such:
Check the running result, for example:
Display data by page using the GridView Control
The GridView control has a built-in paging control that supports basic paging functions. To enable pagination, you must set the AllowPaging and PageSize attributes. For example:
Then, you need to write a PageIndexChanging event of the GridView control, written as follows:
Page_Load (GridViewOne_PageIndexChanging (. GridViewOne. PageIndex =View Code
Run the following result:
Some advanced applications of GridView in programming
The GridView actually contains a family of interesting things, including RowEdtiting, RowUpdating, and RowCancelingEdit events. These events can help us edit, update, and cancel operations, and there is also a RowDeleting event, you can delete a record. The following shows the process:
1. Create an IndexTow. aspx page and place a GridView control in it. Then, use the select, edit, cancel, and delete buttons of the CommandField column of the GridView control.
Now, we start to bind data first.
Of course, we should bind the data when loading. The data binding code is as follows:
Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace DataShowApplication {public partial class IndexTwo: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {// call the binding method this. bindData () ;}/// write a Data binding method private void BindData () {// the old set of connection strings start with string strconn = @ "Data Source =. \ CENA; Initial Catalog = Asp.net; Persist Security Info = True; User ID = sa; Password = 123456 "; // data can be obtained only when SQL statements are written. (In fact, standard SQL statements are basically written in uppercase.) string sqlquery = "select * from DemoStu "; // create the database connection object SqlConnection connection = new SqlConnection (strconn) when you are ready; // create a data adapter SqlDataAdapter da = new SqlDataAdapter (sqlquery, strconn ); // continue to create the data set DataSet ds = new DataSet (); // fill in the data set da. fill (ds); // set the data source of the GridView control to the created data set ds this. gridViewTwo. dataSource = ds; // put the primary key field in the database table into the DataKeyNames attribute of the GridView control. this. gridViewTwo. dataKeyNames = new string [] {"ID"}; // bind the data in the database this. gridViewTwo. dataBind ();}}}
After the data is bound, the running result is as follows:
Then, let's continue to improve. Let's start editing. When we click Edit, we should trigger the RowEditing event of the GridView control. At this time, our edit item index settings should be the index of the selected item, then, bind the data again. The Code is as follows:
Page_Load ((! Strconn = sqlquery = SqlConnection connection = SqlDataAdapter da = DataSet ds =. gridViewTwo. dataSource =. gridViewTwo. dataKeyNames = [] {RowEditing (. gridViewTwo. editIndex = GridViewTwo_RowUpdating (ID = Name = (TextBox )(. gridViewTwo. rows [e. rowIndex]. cells []. controls [Price = (TextBox )(. gridViewTwo. rows [e. rowIndex]. cells []. controls [time = (TextBox )(. gridViewTwo. rows [e. rowIndex]. cells []. controls [update_ SQL = + Name ++ Price ++ time ++ ID + update = ExceSQL (update_ SQL );. gridViewTwo. editIndex =-GridViewTwo_RowCancelingEdit (. gridViewTwo. editIndex =-GridViewTwo_RowDeleting (delete_ SQL = +. gridViewTwo. dataKeys [e. rowIndex]. value. toString () + delete = ExceSQL (delete_ SQL); ExceSQL (strCon = SqlConnection sqlcon = (sqlcon. state = System. data. connectionState. closed)View Code
The following is a brief introduction to the GridView. you can experiment with the same shrimps as me .....
We will continue to learn about the other two commonly used data controls: DataList control and ListView control.