"ASP.--gridview" Data binding

Source: Internet
Author: User

Girdview Introduction:

Name: Network view.

Source: The GridView is the successor of the DataGrid, and in the. NET Framework 2, although there is still a DataGrid, the GridView has embarked on a historical front, and the trend to replace the DataGrid is unstoppable.

Function: The ability to display data from a data source in a Web page. The GridView and DataGrid function Similarly, displaying data from a data source in a Web page, displaying a row of data from a data source, that is, a record, as a row in the output table on a Web page.

Detailed properties and events in this girdview are no longer elaborated. I'm just going to give you a brief introduction to how Girdview displays the data that was searched from the background database, that is, how girdview binds and displays the data source.

First, the front interface is as follows


Second, write in the background: use VS to build an ASP. NET form application. Here, I only write the query function, the background code is as follows

1. Establish database connection

       public static SqlConnection CreateConnection ()        {            SqlConnection con = new SqlConnection ("server=.; database=dropdowntest;uid=sa;pwd=123456 ");            Con. Open ();            return con;        }

2, write the Operation class, which have common query method, conditional Query method, add method (slightly)

        public static DataTable SelectAll ()        {            SqlConnection con = createconnection ();            DataTable dt = new DataTable ();            SqlCommand cmd = new SqlCommand ("SELECT * from person", con);            SqlDataReader SDR = cmd. ExecuteReader ();            Dt. Load (SDR);            return DT;        }

3. Write a query button Click event

        protected void Button4_Click (object sender, EventArgs e) {string c = ""; Defines an empty string that is used to condition query//Set check box 1 for the query condition if (this.   checkbox1.checked) {c = "pid=" + this.txtID.Text;        Exact match query condition} else {c = "PID like '% '"; Fuzzy matching query condition} if (this.            checkbox2.checked) {c + = "and personname like '%" + this.txtName.Text + "% '"; } if (this. checkbox3.checked) {if (radiobutton1.checked) {c + = "and per                sonsex= ' Male ';                } else {c + = "and personsex= ' female '"; }} DataView dv = new DataView (Perosonoperate.selectall ()); Call the Query method DV.                                       RowFilter = C; Set Filters (Find by Criteria) DV.            Sort = "PID Desc";                       Arrange the results in descending order of PID fields Gridview1.datasource = DV;                                   Set data source Gridview1.databind (); Bind the data source//Set the column name, if not set, the corresponding field name in the database instead of gridview1.headerrow.cells[0].            Text = "number"; GRIDVIEW1.HEADERROW.CELLS[1].            Text = "name"; GRIDVIEW1.HEADERROW.CELLS[2].          Text = "gender";

three queries are as follows: Direct click Query, gender query, by number and name and gender.


The background code written above is only in the function can be implemented on the basis of the written, there are some bugs, I hope you change their own.

From the background binding data filtering to the foreground rendering, with the GridView data in the browser to display the approximate process is this, the only thing is a bit awkward in terms of query, the spelling of the string is not good, so it is nothing but the use of Girdview filter effect, that is, this code Dv. RowFilter = C; I hope you will be more careful in code writing.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"ASP.--gridview" Data binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.