"ASP.--gridview" Data binding

Source: Internet
Author: User

Girdview Brief Introduction:

Name: Network view.

Source: The GridView is the successor control of the DataGrid. In the. NET Framework 2, despite the presence of the DataGrid, the GridView has embarked on a historical front, and the trend in place of the DataGrid has been unstoppable.

Function: The ability to display data from a data source in a Web page. The GridView and DataGrid feature similar features, where data from a data source is displayed in a Web page, and a row of data from the data source is presented. That is, a record that appears as a row in the output table on a Web page.

The specific properties and events of this girdview I will not elaborate. The following is just a brief introduction to how Girdview displays data that was searched from a background database. That is how girdview binds and displays the data source.

First, the front interface such as the following


Second, background writing: Use VS to build ASP.

Here, I just write the query function, background code such as the following

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, there are common query methods, conditional query method, join 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 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";                       Make the result in descending order of pid field gridview1.datasource = DV;                                   Set data source Gridview1.databind (); Bind the data source//Set the column name, assuming that it is not set, will replace Gridview1.headerrow.cells[0] with the corresponding field name in the database.            Text = "number"; GRIDVIEW1.HEADERROW.CELLS[1].            Text = "name"; GRIDVIEW1.HEADERROW.CELLS[2].          Text = "gender";

three queries, such as the one below, are direct click Queries, gender-based inquiries, numbers and names and gender.


The background code written above is only written on the basis of the function can be implemented, there are some bugs. We hope that we will transform ourselves.

From the background bound data filtering to the foreground rendering, with the GridView data in the browser to display the approximate process is this. The only thing that's a little awkward is that in terms of query, the spelling of the string is not good, so it is nothing more than using Girdview filter effect. This is the code DV. RowFilter = C; I hope you will be more careful in code writing.

"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.